ループが完了するか、設定された時間間隔が経過したときに実行されるまで、特定の時間間隔で実行されるプロシージャを作成したい場合があります。 Timerコンポーネントは、このような手順を可能にします。
このコンポーネントは、Windows フォーム環境向けに設計されています。 サーバー環境に適したタイマーが必要な場合は、「Server-Based タイマーの概要
注
Timer コンポーネントを使用する場合は、いくつかの制限があります。 詳細については、「 Windows フォーム タイマー コンポーネントの Interval プロパティの制限事項」を参照してください。
Timer コンポーネントを使用して設定された間隔でプロシージャを実行するには
フォームに Timer を追加します。 これをプログラムで行う方法の図については、次の例のセクションを参照してください。 Visual Studio には、フォームへのコンポーネントの追加もサポートされています。 「 方法: ユーザー インターフェイスのないコントロールを Windows フォームに追加する」も参照してください。
タイマーの Interval プロパティ (ミリ秒単位) を設定します。 このプロパティは、プロシージャが再度実行されるまでの時間を決定します。
注
タイマー イベントが発生する頻度が高いほど、イベントへの応答に使用されるプロセッサ時間が長くなります。 これにより、全体的なパフォーマンスが低下する可能性があります。 必要以上に短い間隔を設定しないでください。
Tick イベント ハンドラーに適切なコードを記述します。 このイベントで記述するコードは、 Interval プロパティで指定された間隔で実行されます。
タイマーを開始するには、 Enabled プロパティを
true
に設定します。 Tick イベントが発生し始め、設定された間隔でプロシージャが実行されます。適切なタイミングで、 Enabled プロパティを
false
に設定して、プロシージャの再実行を停止します。 間隔を0
に設定しても、タイマーは停止しません。
最初のコード例
この最初のコード例では、時刻を 1 秒単位で追跡します。 フォーム上の Button、 Label、および Timer コンポーネントを使用します。
Interval プロパティは 1000 (1 秒に等しい) に設定されます。
Tick イベントでは、ラベルのキャプションは現在の時刻に設定されます。 ボタンをクリックすると、 Enabled プロパティが false
に設定され、タイマーがラベルのキャプションを更新できなくなります。 次のコード例では、Buttonという名前のButton1
コントロール、Timerという名前のTimer1
コントロール、および Label という名前のLabel1
コントロールを持つフォームが必要です。
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()
{
// Call this procedure when the application starts.
// Set to 1 second.
Timer1.Interval = 1000;
Timer1.Tick += new EventHandler(Timer1_Tick);
// Enable timer.
Timer1.Enabled = true;
Button1.Text = "Stop";
Button1.Click += new EventHandler(Button1_Click);
}
private void Timer1_Tick(object Sender, EventArgs e)
{
// Set the caption to the current time.
Label1.Text = DateTime.Now.ToString();
}
private void Button1_Click(object sender, EventArgs e)
{
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->Interval = 1000;
// Enable timer.
timer1->Enabled = true;
this->timer1->Tick += gcnew System::EventHandler(this,
&Form1::timer1_Tick);
button1->Text = S"Stop";
this->button1->Click += gcnew System::EventHandler(this,
&Form1::button1_Click);
}
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 番目のコード例
この 2 番目のコード例では、ループが完了するまで 600 ミリ秒ごとにプロシージャを実行します。 次のコード例では、Buttonという名前のButton1
コントロール、Timerという名前のTimer1
コントロール、および Label という名前のLabel1
コントロールを持つフォームが必要です。
' 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();
}
}
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());
}
}
こちらも参照ください
.NET Desktop feedback