Freigeben über


Timer.Stop Methode

Definition

Beendet den Timer.

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

Beispiele

Im folgenden Codebeispiel wird ein einfacher Intervalltimer implementiert, der alle fünf Sekunden einen Alarm aussetzt. Wenn der Alarm auftritt, wird eine MessageBox Anzahl der Häufigkeit angezeigt, mit der der Alarm gestartet wurde, und fordert den Benutzer auf, ob der Timer weiterhin ausgeführt werden soll.

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

Hinweise

Sie können den Timer auch beenden, indem Sie die Enabled Eigenschaft auf false. Ein Timer Objekt kann innerhalb derselben Anwendungssitzung mehrmals aktiviert und deaktiviert werden.

Wenn Start Sie einen Timer Anruf Stop deaktiviert haben, wird das Timer unterbrochene Intervall neu gestartet. Wenn Der Timer Anruf für ein 5000-Millisekunden-Intervall festgelegt ist und Sie etwa 3000 Millisekunden aufrufenStop, bewirkt der Aufruf, dass der Timer Anruf Start 5000 Millisekunden wartet, bevor das Tick Ereignis ausgelöst wird.

Hinweis

Das Aufrufen von "Beenden" Timer in einer Windows Forms-Anwendung kann dazu führen, dass Nachrichten von anderen Timer Komponenten in der Anwendung sofort verarbeitet werden, da alle Timer Komponenten im Hauptanwendungsthread ausgeführt werden. Wenn Sie über zwei Timer Komponenten verfügen, wird eine auf 700 Millisekunden und eine auf 500 Millisekunden festgelegt, und Sie rufen zuerst die erste TimeraufStop, ihre Anwendung erhält möglicherweise einen Ereignisrückruf für die zweite Komponente. Wenn dies problematisch ist, sollten Sie stattdessen die Verwendung der Timer Klasse im System.Threading Namespace in Betracht ziehen.

Gilt für:

Weitere Informationen