Timer.Enabled プロパティ
アセンブリ: System (system.dll 内)
構文
/** @property */ public boolean get_Enabled () /** @property */ public void set_Enabled (boolean value)
Timer で Elapsed イベントを発生させる場合は true。それ以外の場合は false。既定値は false です。
Enabled を true に設定することは、Start を呼び出すことと同じであり、Enabled を false に設定することは、Stop を呼び出すことと同じです。
メモ |
---|
Elapsed イベントは ThreadPool のスレッドで発生するため、1 つのスレッドでイベント処理メソッドを実行している間に、別のスレッドで Enabled プロパティが false に設定される可能性があります。これにより、Enabled プロパティが false に設定された後に Elapsed イベントが発生する場合があります。Stop メソッドのコード例では、この競合状態を回避する方法の一例を示しています。 |
Enabled を true に設定し、AutoReset を false に設定した場合、Timer は最初に間隔が経過したときに、1 回だけ Elapsed イベントを発生させます。
Timer が開始した後に間隔を設定すると、カウントがリセットされます。たとえば、間隔を 5 秒に設定し、Enabled を true に設定した場合は、Enabled を設定した時刻からカウントが開始します。カウントが 3 秒のときに間隔を 10 秒にリセットした場合、最初の Elapsed イベントは Enabled を true に設定してから 13 秒後に発生します。
5 秒ごとに、コンソールに "Hello World!" と表示する Timer を作成する例を次に示します。
この例では、System.Timers 名前空間を使用します。
Imports System Imports System.Timers Public Class Timer1 Public Shared Sub Main() ' Normally, the timer is declared at the class level, so ' that it doesn't go out of scope when the method ends. ' In this example, the timer is needed only while Main ' is executing. However, KeepAlive must be used at the ' end of Main, to prevent the JIT compiler from allowing ' aggressive garbage collection to occur before Main ' ends. Dim aTimer As New System.Timers.Timer() ' Hook up the Elapsed event for the timer. AddHandler aTimer.Elapsed, AddressOf OnTimedEvent ' Set the Interval to 2 seconds (2000 milliseconds). aTimer.Interval = 2000 aTimer.Enabled = True Console.WriteLine("Press the Enter key to exit the program.") Console.ReadLine() ' Keep the timer alive until the end of Main. GC.KeepAlive(aTimer) End Sub ' Specify what you want to happen when the Elapsed event is ' raised. Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs) Console.WriteLine("Hello World!") End Sub End Class
using System; using System.Timers; public class Timer1 { public static void Main() { // Normally, the timer is declared at the class level, so // that it doesn't go out of scope when the method ends. // In this example, the timer is needed only while Main // is executing. However, KeepAlive must be used at the // end of Main, to prevent the JIT compiler from allowing // aggressive garbage collection to occur before Main // ends. System.Timers.Timer aTimer = new System.Timers.Timer(); // Hook up the Elapsed event for the timer. aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); // Set the Interval to 2 seconds (2000 milliseconds). aTimer.Interval = 2000; aTimer.Enabled = true; Console.WriteLine("Press the Enter key to exit the program."); Console.ReadLine(); // Keep the timer alive until the end of Main. GC.KeepAlive(aTimer); } // Specify what you want to happen when the Elapsed event is // raised. private static void OnTimedEvent(object source, ElapsedEventArgs e) { Console.WriteLine("Hello World!"); } }
#using <system.dll> using namespace System; using namespace System::Timers; public ref class Timer1 { public: static void Demo() { // Normally, the timer is declared at the class level, so // that it doesn't go out of scope when the method ends. // In this example, the timer is needed only while Demo // is executing. However, KeepAlive must be used at the // end of Demo, to prevent the JIT compiler from allowing // aggressive garbage collection to occur before Demo // ends. System::Timers::Timer^ aTimer = gcnew System::Timers::Timer; // Hook up the Elapsed event for the timer. aTimer->Elapsed += gcnew ElapsedEventHandler( Timer1::OnTimedEvent ); // Set the Interval to 2 seconds (2000 milliseconds). aTimer->Interval = 2000; aTimer->Enabled = true; Console::WriteLine("Press the Enter key to exit the program."); Console::ReadLine(); // Keep the timer alive until the end of the Demo method. GC::KeepAlive(aTimer); } private: // Specify what you want to happen when the Elapsed event is // raised. static void OnTimedEvent( Object^ /*source*/, ElapsedEventArgs^ /*e*/ ) { Console::WriteLine( "Hello World!" ); } }; int main() { Timer1::Demo(); }
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Timer.Enabled プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文
/** @property */ public boolean get_Enabled () /** @property */ public void set_Enabled (boolean value)
タイマが現在有効な場合は true。それ以外の場合は false。既定値は false です。
値が true の場合、タイマはガベージ コレクションの対象ではありません。
5 秒ごとにアラームを発生させる単純な間隔タイマを実装する例を次に示します。アラームが発生すると、MessageBox にアラームが起動した回数が表示され、タイマの実行を継続するかどうかをユーザーに問い合わせるメッセージが表示されます。
Public Class Class1 Private Shared myTimer As New System.Windows.Forms.Timer() Private Shared alarmCounter As Integer = 1 Private Shared exitFlag As Boolean = False ' This is the method to run when the timer is raised. Private Shared Sub TimerEventProcessor(myObject As Object, _ myEventArgs As EventArgs) myTimer.Stop() ' Displays a message box asking whether to continue running the timer. If MessageBox.Show("Continue running?", "Count is: " & alarmCounter, _ MessageBoxButtons.YesNo) = DialogResult.Yes Then ' Restarts the timer and increments the counter. alarmCounter += 1 myTimer.Enabled = True Else ' Stops the timer. exitFlag = True End If End Sub Public Shared Sub Main() ' Adds the event and the event handler for the method that will ' process the timer event to the timer. AddHandler myTimer.Tick, AddressOf TimerEventProcessor ' Sets the timer interval to 5 seconds. myTimer.Interval = 5000 myTimer.Start() ' Runs the timer, and raises the event. While exitFlag = False ' Processes all the events in the queue. Application.DoEvents() End While End Sub End Class
public class Class1 { static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer(); static int alarmCounter = 1; static bool exitFlag = false; // This is the method to run when the timer is raised. private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs) { myTimer.Stop(); // Displays a message box asking whether to continue running the timer. if(MessageBox.Show("Continue running?", "Count is: " + alarmCounter, MessageBoxButtons.YesNo) == DialogResult.Yes) { // Restarts the timer and increments the counter. alarmCounter +=1; myTimer.Enabled = true; } else { // Stops the timer. exitFlag = true; } } public static int Main() { /* Adds the event and the event handler for the method that will process the timer event to the timer. */ myTimer.Tick += new EventHandler(TimerEventProcessor); // Sets the timer interval to 5 seconds. myTimer.Interval = 5000; myTimer.Start(); // Runs the timer, and raises the event. while(exitFlag == false) { // Processes all the events in the queue. Application.DoEvents(); } return 0; } }
public ref class Class1 { private: static System::Windows::Forms::Timer^ myTimer = gcnew System::Windows::Forms::Timer; static int alarmCounter = 1; static bool exitFlag = false; // This is the method to run when the timer is raised. static void TimerEventProcessor( Object^ /*myObject*/, EventArgs^ /*myEventArgs*/ ) { myTimer->Stop(); // Displays a message box asking whether to continue running the timer. if ( MessageBox::Show( "Continue running?", String::Format( "Count is: {0}", alarmCounter ), MessageBoxButtons::YesNo ) == DialogResult::Yes ) { // Restarts the timer and increments the counter. alarmCounter += 1; myTimer->Enabled = true; } else { // Stops the timer. exitFlag = true; } } public: static void Main() { /* Adds the event and the event handler for the method that will process the timer event to the timer. */ myTimer->Tick += gcnew EventHandler( TimerEventProcessor ); // Sets the timer interval to 5 seconds. myTimer->Interval = 5000; myTimer->Start(); // Runs the timer, and raises the event. while ( exitFlag == false ) { // Processes all the events in the queue. Application::DoEvents(); } } }; int main() { Class1::Main(); }
public class Class1 { private static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer(); private static int alarmCounter = 1; private static boolean exitFlag = false; // This is the method to run when the timer is raised. private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs) { myTimer.Stop(); // Displays a message box asking whether to continue running the timer. if (MessageBox.Show("Continue running?", "Count is: " + alarmCounter, MessageBoxButtons.YesNo).Equals(DialogResult.Yes)) { // Restarts the timer and increments the counter. alarmCounter += 1; myTimer.set_Enabled(true); } else { // Stops the timer. exitFlag = true; } } //TimerEventProcessor public static void main(String[] args) { /* Adds the event and the event handler for the method that will process the timer event to the timer. */ myTimer.add_Tick(new EventHandler(TimerEventProcessor)); // Sets the timer interval to 5 seconds. myTimer.set_Interval(5000); myTimer.Start(); // Runs the timer, and raises the event. while (exitFlag == false) { // Processes all the events in the queue. Application.DoEvents(); } return; } //main } //Class1
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- Timer.Enabled プロパティのページへのリンク