次の方法で共有


Timer クラス

定義

ユーザー定義の間隔でイベントを発生させるタイマーを実装します。 このタイマーは、Windows フォーム アプリケーションで使用するために最適化されており、ウィンドウで使用する必要があります。

public ref class Timer : System::ComponentModel::Component
public class Timer : System.ComponentModel.Component
type Timer = class
    inherit Component
Public Class Timer
Inherits Component
継承

次の例では、5 秒ごとにアラームをオフにする単純な間隔タイマーを実装します。 アラームが発生すると、 MessageBox はアラームが開始された回数のカウントを表示し、タイマーの実行を継続するかどうかをユーザーに求めます。

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 )
      {
         
         // Processes all the events in the queue.
         Application::DoEvents();
      }
   }

};

int main()
{
   Class1::Main();
}
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) {
          // Processes all the events in the queue.
          Application.DoEvents();
       }
    return 0;
    }
 }
Public Class Class1
    Private Shared WithEvents 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, _
                                           ByVal myEventArgs As EventArgs) _
                                       Handles myTimer.Tick
        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.
        
        ' 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

注釈

Timerは、ユーザー定義の間隔でイベントを発生させるために使用されます。 この Windows タイマーは、UI スレッドを使用して処理を実行するシングル スレッド環境向けに設計されています。 ユーザー コードで UI メッセージ ポンプを使用でき、常に同じスレッドから動作するか、呼び出しを別のスレッドにマーシャリングする必要があります。

このタイマーを使用する場合は、 Tick イベントを使用してポーリング操作を実行するか、指定した期間のスプラッシュ画面を表示します。 Enabled プロパティが true に設定され、Interval プロパティが 0 より大きい場合、Tick イベントは、Interval プロパティの設定に基づいて間隔を置いて発生します。

このクラスは、間隔を設定し、タイマーを開始および停止するメソッドを提供します。

Windows フォーム タイマー コンポーネントはシングル スレッドであり、55 ミリ秒の精度に制限されています。 より精度の高いマルチスレッド タイマーが必要な場合は、System.Timers名前空間の Timer クラスを使用します。

コンストラクター

名前 説明
Timer()

Timer クラスの新しいインスタンスを初期化します。

Timer(IContainer)

指定したコンテナーと共に、 Timer クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
CanRaiseEvents

コンポーネントがイベントを発生できるかどうかを示す値を取得します。

(継承元 Component)
Container

IContainerを含むComponentを取得します。

(継承元 Component)
DesignMode

Componentが現在デザイン モードであるかどうかを示す値を取得します。

(継承元 Component)
Enabled

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

Events

この Componentにアタッチされているイベント ハンドラーの一覧を取得します。

(継承元 Component)
Interval

Tick イベントの最後の発生を基準にして、Tick イベントが発生するまでの時間をミリ秒単位で取得または設定します。

Site

ISiteComponentを取得または設定します。

(継承元 Component)
Tag

何らかの種類のユーザー状態を表す任意の文字列を取得または設定します。

メソッド

名前 説明
CreateObjRef(Type)

リモート オブジェクトとの通信に使用されるプロキシの生成に必要なすべての関連情報を含むオブジェクトを作成します。

(継承元 MarshalByRefObject)
Dispose()

Componentによって使用されるすべてのリソースを解放します。

(継承元 Component)
Dispose(Boolean)

タイマーによって使用されるメモリ以外のリソースを破棄します。

Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetLifetimeService()
古い.

このインスタンスの有効期間ポリシーを制御する現在の有効期間サービス オブジェクトを取得します。

(継承元 MarshalByRefObject)
GetService(Type)

ComponentまたはそのContainerによって提供されるサービスを表すオブジェクトを返します。

(継承元 Component)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
InitializeLifetimeService()
古い.

このインスタンスの有効期間ポリシーを制御する有効期間サービス オブジェクトを取得します。

(継承元 MarshalByRefObject)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
MemberwiseClone(Boolean)

現在の MarshalByRefObject オブジェクトの簡易コピーを作成します。

(継承元 MarshalByRefObject)
OnTick(EventArgs)

Tick イベントを発生させます。

Start()

タイマーを開始します。

Stop()

タイマーを停止します。

ToString()

Timerを表す文字列を返します。

イベント

名前 説明
Disposed

コンポーネントが Dispose() メソッドの呼び出しによって破棄されるときに発生します。

(継承元 Component)
Tick

指定したタイマー間隔が経過し、タイマーが有効になっているときに発生します。

適用対象