次の方法で共有


方法 : Windows フォームの Timer コンポーネントを使用して一定間隔でプロシージャを実行する

更新 : 2007 年 11 月

場合により、ループが終了するまで一定の時間間隔で実行されるプロシージャや、指定された時間間隔が経過したときに実行されるプロシージャを作成する必要があります。Timer コンポーネントを使用して、そのようなプロシージャを実現できます。

このコンポーネントは、Windows フォーム環境で使用します。サーバー環境に適したタイマが必要な場合は、「サーバー ベースのタイマの概説」を参照してください。

3tszykws.alert_note(ja-jp,VS.90).gifメモ :

Timer コンポーネントを使用する場合には、いくつかの制限があります。詳細については、「Windows フォームの Timer コンポーネントの Interval プロパティの制限」を参照してください。

Timer コンポーネントを使用して一定の間隔でプロシージャを実行するには

  1. フォームに Timer を追加します。これをプログラムで行う方法については、次のサンプル セクションの説明を参照してください。Visual Studio ではコンポーネントをフォームに追加することもできます。詳細については方法 : ユーザー インターフェイスを持たないコントロールを Windows フォームに追加する および方法 : ユーザー インターフェイスを持たないコントロールを Windows フォームに追加する および方法 : ユーザー インターフェイスを持たないコントロールを Windows フォームに追加する および方法 : ユーザー インターフェイスを持たないコントロールを Windows フォームに追加する.

  2. タイマの Interval プロパティ (ミリ秒単位) を設定します。このプロパティは、プロシージャが次に実行されるまでの時間を決定します。

    3tszykws.alert_note(ja-jp,VS.90).gifメモ :

    タイマ イベントの発生間隔が頻繁であるほど、イベントに応答するために多くのプロセッサ時間が使用されます。そのため、全体のパフォーマンスが低下する可能性があります。必要以上に短い間隔を設定しないでください。

  3. Tick イベント ハンドラに適切なコードを記述します。このイベントに記述したコードは、Interval プロパティに指定された間隔で実行されます。

  4. タイマを起動するには、Enabled プロパティを true に設定します。Tick イベントの発生が開始され、プロシージャが指定された間隔で実行されます。

  5. 適切な時点で、Enabled プロパティを false に設定してプロシージャの実行を停止します。間隔を 0 に設定してもタイマは停止しません。

使用例

この最初のコード例では、時刻を 1 秒間隔で追跡します。このコードでは、フォーム上で ButtonLabel、および Timer コンポーネントが使用されます。Interval プロパティは 1000 (= 1 秒) に設定されます。Tick イベントでは、ラベルのキャプションが現在の時刻に設定されます。ボタンがクリックされると、Enabled プロパティが false に設定され、タイマはラベルのキャプションの更新を停止します。次のコード例では、 Button1 という名前の Button コントロール、 Timer1 という名前の Timer コントロール、および Label1 という名前の Label コントロールを持つフォームが必要です。

Private Sub InitializeTimer()
   ' Run this procedure in an appropriate event.
   ' Set to 1 second.
   Timer1.Interval = 1000
   ' Enable timer.
   Timer1.Enabled = True
   Button1.Text = "Enabled"
End Sub
x
Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
' Set the caption to the current time.
   Label1.Text = DateTime.Now
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      If Button1.Text = "Stop" Then
         Button1.Text = "Start"
         Timer1.Enabled = False
      Else
         Button1.Text = "Stop"
         Timer1.Enabled = True
      End If
End Sub
private void InitializeTimer()
{
   //' Run this procedure in an appropriate event.
   // Set to 1 second.
   Timer1.Interval = 1000;
   // Enable timer.
   Timer1.Enabled = true;
   Button1.Text = "Stop";
}

private void Timer1_Tick(object Sender, EventArgs e)   
{
   // Set the caption to the current time.
   Label1.Text = DateTime.Now.ToString();
}

private void Button1_Click()
{
  if ( Button1.Text == "Stop" )
  {
    Button1.Text = "Start";
    Timer1.Enabled = false;
  }
  else
  {
    Button1.Text = "Stop";
    Timer1.Enabled = true;
  }
}
private void InitializeTimer() 
{
   // Run this procedure in an appropriate event.
   // Set to 1 second.
   Timer1.set_Interval(1000);
   // Enable timer.
   Timer1.set_Enabled(true);
   Button1.set_Text("Stop");
}

private void Timer1_Tick(System.Object Sender, EventArgs e) 
{
   // Set the caption to the current time.
   Label1.set_Text(DateTime.get_Now().ToString());
}

private void Button1_Click() 
{
   if ( Button1.get_Text() == "Stop"  ) 
   {
      Button1.set_Text("Start");
      Timer1.set_Enabled(false);
   }
   else
   {
      Button1.set_Text("Stop");
      Timer1.set_Enabled(true);
   }
}
private:
   void InitializeTimer()
   {
      // Run this procedure in an appropriate event.
      // Set to 1 second.
      timer1->Interval = 1000;
      // Enable timer.
      timer1->Enabled = true;
      button1->Text = S"Stop";
   }

   void timer1_Tick(System::Object ^ sender,
      System::EventArgs ^ e)
   {
      // Set the caption to the current time.
      label1->Text = DateTime::Now.ToString();
   }

   void button1_Click(System::Object ^ sender,
      System::EventArgs ^ e)
   {
      if ( button1->Text == "Stop" )
      {
         button1->Text = "Start";
         timer1->Enabled = false;
      }
      else
      {
         button1->Text = "Stop";
         timer1->Enabled = true;
      }
   }

この 2 つ目のコード例では、ループが終了するまでの間、プロシージャが 600 ミリ秒ごとに実行されます。次のコード例では、 Button1 という名前の Button コントロール、 Timer1 という名前の Timer コントロール、および Label1 という名前の Label コントロールを持つフォームが必要です。

' This variable will be the loop counter.
Private counter As Integer

Private Sub InitializeTimer()
   ' Run this procedure in an appropriate event.
   counter = 0
   Timer1.Interval = 600
   Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
   If counter => 10 Then
      ' Exit loop code.
      Timer1.Enabled = False
      counter = 0
   Else
      ' Run your procedure here.
      ' Increment counter.
      counter = counter + 1
      Label1.Text = "Procedures Run: " & counter.ToString
   End If
End Sub
// This variable will be the loop counter.
private int counter;

private void InitializeTimer()
{
   // Run this procedure in an appropriate event.
   counter = 0;
   timer1.Interval = 600;
   timer1.Enabled = true;
   // Hook up timer's tick event handler.
   this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
}

private void timer1_Tick(object sender, System.EventArgs e)   
{
   if (counter >= 10) 
   {
      // Exit loop code.
      timer1.Enabled = false;
      counter = 0;
   }
   else
   {
      // Run your procedure here.
      // Increment counter.
      counter = counter + 1;
      label1.Text = "Procedures Run: " + counter.ToString();
      }
}
// Run this procedure in an appropriate event.
counter = 0;
timer1.set_Interval(600);
timer1.set_Enabled(true);
// Wire up timer's tick event handler.
this.timer1.add_Tick(new System.EventHandler(this.timer1_Tick));

private void timer1_Tick(System.Object sender, System.EventArgs e) 
{
   if ( counter >= 10  ) 
   {
      // Exit loop code.
      timer1.set_Enabled(false);
      counter = 0;
   }
   else
   {
      // Run your procedure here.
      // Increment counter.
      counter = counter + 1;
      this.timer1.add_Tick(new System.EventHandler(this.timer1_Tick));
   }
}
private:
   int counter;

   void InitializeTimer()
   {
      // Run this procedure in an appropriate event.
      counter = 0;
      timer1->Interval = 600;
      timer1->Enabled = true;
      // Hook up timer's tick event handler.
      this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
   }

   void timer1_Tick(System::Object ^ sender,
      System::EventArgs ^ e)
   {
      if (counter >= 10) 
      {
         // Exit loop code.
         timer1->Enabled = false;
         counter = 0;
      }
      else
      {
         // Run your procedure here.
         // Increment counter.
         counter = counter + 1;
         label1->Text = String::Concat("Procedures Run: ",
            counter.ToString());
      }
   }

参照

参照

Timer コンポーネントの概要 (Windows フォーム)

Timer

その他の技術情報

Timer コンポーネント (Windows フォーム)