Timer.Stop 메서드

정의

타이머를 중지합니다.

public:
 void Stop();
public void Stop ();
member this.Stop : unit -> unit
Public Sub Stop ()

예제

다음 코드 예제에서는 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 == false )
      {
         
         // 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 == false) {
          // 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

설명

속성을 로 설정하여 타이머를 Enabled 중지할 false수도 있습니다. Timer 개체를 사용 하도록 설정 하 고 동일한 애플리케이션 세션 내에서 여러 번 사용 하지 않도록 설정 수 있습니다.

를 호출 Start 하여 를 Timer 사용하지 않도록 설정한 후 를 호출 Stop 하면 Timer 가 중단된 간격을 다시 시작합니다. Timer 가 5,000밀리초 간격으로 설정되어 있고 약 3,000밀리초 단위로 호출 Stop 하는 경우 를 호출 Start 하면 가 이벤트를 발생 Tick 하기 전에 5,000밀리초를 대기하게 됩니다Timer.

참고

중지를 호출 Timer 내 Windows Forms 애플리케이션 메시지가 될 수 있습니다 다른 Timer 때문에 즉시 처리 되도록 애플리케이션의 구성 요소 모든 Timer 구성 요소는 기본 애플리케이션 스레드에서 작동 합니다. 두 개 있는 경우 Timer 구성 요소, 700 밀리초에서 500 밀리초로 하나의 집합으로 설정 하나 및 문의할 Stop 첫 번째 Timer, 애플리케이션은 두 번째 구성 요소에 대 한 이벤트 콜백을 먼저 나타날 수 있습니다. 문제가 있는 경우 대신 네임스페이 Timer 스에서 클래스를 System.Threading 사용하는 것이 좋습니다.

적용 대상

추가 정보