次の方法で共有


Timer.Enabled プロパティ

タイマが実行されているかどうかを取得または設定します。

Public Overridable Property Enabled As Boolean
[C#]
public virtual bool Enabled {get; set;}
[C++]
public: __property virtual bool get_Enabled();public: __property virtual void set_Enabled(bool);
[JScript]
public function get Enabled() : Boolean;public function set Enabled(Boolean);

プロパティ値

タイマが現在有効な場合は true 。それ以外の場合は false 。既定値は false です。

解説

値が true の場合、タイマはガベージ コレクションの対象ではありません。

使用例

[Visual Basic, C#, C++] 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


[C#] 
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;
    }
 }
    

[C++] 
public __gc 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(S"Continue running?", String::Format( S"Count is: {0}", __box(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 += new EventHandler(0, 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();
}    

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

Timer クラス | Timer メンバ | System.Windows.Forms 名前空間