다음을 통해 공유


방법: Windows Forms Timer 구성 요소를 사용하여 설정된 간격마다 프로시저 실행

업데이트: 2007년 11월

루프가 끝날 때까지 특정 시간 간격으로 실행되거나 설정된 시간 간격이 경과하면 실행되는 프로시저를 만들어야 할 경우가 종종 있습니다. Timer 구성 요소를 사용하면 이러한 프로시저를 만들 수 있습니다.

이 구성 요소는 Windows Forms 환경용으로 디자인되었습니다. 서버 환경에 적합한 타이머가 필요한 경우에는 서버 기반 타이머 소개를 참조하십시오.

참고:

Timer 구성 요소를 사용할 때는 몇 가지 제한 사항이 있습니다. 자세한 내용은 Windows Forms Timer 구성 요소의 Interval 속성에 대한 제한 사항을 참조하십시오.

Timer 구성 요소를 사용하여 설정된 간격마다 프로시저를 실행하려면

  1. Timer를 폼에 추가합니다. 프로그래밍 방식으로 추가하는 방법에 대한 그림은 다음 예제 부분을 참조하십시오. Visual Studio에서는 폼에 구성 요소를 추가할 수도 있습니다.

  2. 타이머에 대한 Interval 속성을 밀리초 단위로 설정합니다. 이 속성은 프로시저가 다시 실행되기 전까지 경과하는 시간을 결정합니다.

    참고:

    타이머 이벤트가 자주 발생할수록 이 이벤트에 응답하는 데 사용되는 프로세서 시간이 늘어납니다. 이렇게 되면 전반적인 성능이 떨어질 수 있습니다. 간격을 필요한 시간보다 짧게 설정하지 마십시오.

  3. Tick 이벤트 처리기에서 적절한 코드를 작성합니다. 이 이벤트에서 작성한 코드는 Interval 속성에 지정된 간격마다 실행됩니다.

  4. Enabled 속성을 true로 설정하여 타이머를 시작합니다. 설정된 간격마다 프로시저를 실행하는 Tick 이벤트가 시작됩니다.

  5. 적절한 시간에 Enabled 속성을 false로 설정하여 프로시저가 다시 실행되지 않도록 합니다. 간격을 0으로 설정한다고 해서 타이머가 멈추는 것은 아닙니다.

예제

이 첫 번째 코드 예제는 매초마다 시간을 추적합니다. 이 예제에서는 폼에 Button, LabelTimer 구성 요소를 사용합니다. 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()
{
   //' 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;
      }
   }

이 두 번째 코드 예제는 루프가 끝날 때까지 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();
      }
}
// 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 Forms)

Timer

기타 리소스

Timer 구성 요소(Windows Forms)