Freigeben über


Timer-Klasse

Implementiert einen Zeitgeber, der ein Ereignis in benutzerdefinierten Intervallen auslöst. Dieser Zeitgeber ist für die Verwendung in Windows Forms-Anwendungen optimiert und muss in einem Fenster verwendet werden.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Class Timer
    Inherits Component
'Usage
Dim instance As Timer
public class Timer : Component
public ref class Timer : public Component
public class Timer extends Component
public class Timer extends Component

Hinweise

Ein Timer wird verwendet, um ein Ereignis in benutzerdefinierten Intervallen auszulösen. Dieser Windows-Zeitgeber wurde für eine Singlethread-Umgebung entwickelt, in der Benutzeroberflächenthreads für die Verarbeitung verwendet werden. Hierfür muss im Benutzercode eine Nachrichtenverteilschleife für die Benutzeroberfläche verfügbar sein. Die Ausführung muss immer aus demselben Thread erfolgen, oder der Aufruf muss zu einem anderen Thread gemarshallt werden.

Verwenden Sie das Tick-Ereignis zusammen mit diesem Zeitgeber, um eine Abrufoperation durchzuführen oder einen Begrüßungsbildschirm für einen angegebenen Zeitraum einzublenden. Wenn die Enabled-Eigenschaft auf true festgelegt und die Interval-Eigenschaft größer als 0 (null) ist, wird das Tick-Ereignis in dem in der Interval-Eigenschaft festgelegten Intervall ausgelöst.

Diese Klasse stellt Methoden bereit, mit denen der Intervall festgelegt sowie der Zeitgeber gestartet und beendet wird.

Hinweis

Die Zeitgeberkomponente in Windows Forms ist Singlethreaded und auf eine Genauigkeit von 55 Millisekunden beschränkt. Wenn Sie einen Multithread-Zeitgeber mit größerer Genauigkeit benötigen, verwenden Sie die Timer-Klasse im System.Timers-Namespace.

Beispiel

Im folgenden Beispiel wird ein einfacher Intervallzeitgeber implementiert, der alle fünf Sekunden einen Alarm auslöst. Beim Auslösen des Alarms wird in einer MessageBox angezeigt, wie oft der Alarm aktiviert wurde, und der Benutzer wird gefragt, ob der Zeitgeber weiterhin ausgeführt werden soll.

Public Class Class1
    Private Shared 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, _
                                           myEventArgs As EventArgs)
        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.
        AddHandler myTimer.Tick, AddressOf 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()
        End While

    End Sub    

End Class
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 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
{
    private static System.Windows.Forms.Timer myTimer = 
        new System.Windows.Forms.Timer();
    private static int alarmCounter = 1;
    private static boolean 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).Equals(DialogResult.Yes)) {
            // Restarts the timer and increments the counter.
            alarmCounter += 1;
            myTimer.set_Enabled(true);
        }
        else {
            // Stops the timer.
            exitFlag = true;
        }
    } //TimerEventProcessor

    public static void main(String[] args)
    {
        /* Adds the event and the event handler for the method that will 
           process the timer event to the timer. 
         */
        myTimer.add_Tick(new EventHandler(TimerEventProcessor));

        // Sets the timer interval to 5 seconds.
        myTimer.set_Interval(5000);
        myTimer.Start();

        // Runs the timer, and raises the event.
        while (exitFlag == false) {
            // Processes all the events in the queue.
            Application.DoEvents();
        }
        return;
    } //main
} //Class1

Vererbungshierarchie

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
      System.Windows.Forms.Timer

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Timer-Member
System.Windows.Forms-Namespace