Freigeben über


Timer.Enabled-Eigenschaft

Ruft einen Wert ab, der angibt, ob Timer das Elapsed-Ereignis auslösen soll, oder legt diesen fest.

Namespace: System.Timers
Assembly: System (in system.dll)

Syntax

'Declaration
Public Property Enabled As Boolean
'Usage
Dim instance As Timer
Dim value As Boolean

value = instance.Enabled

instance.Enabled = value
public bool Enabled { get; set; }
public:
property bool Enabled {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_Enabled ()

/** @property */
public void set_Enabled (boolean value)
public function get Enabled () : boolean

public function set Enabled (value : boolean)

Eigenschaftenwert

true, wenn Timer das Elapsed-Ereignis auslösen soll, andernfalls false. Der Standardwert ist false.

Hinweise

Das Festlegen von Enabled auf true entspricht dem Aufruf von Start, während das Festlegen von Enabled auf false dem Aufruf von Stop entspricht.

Hinweis

Das Elapsed-Ereignis wird in einem ThreadPool-Thread ausgelöst. Deshalb kann die Ereignisbehandlungsmethode in einem Thread ausgeführt werden, während die Enabled-Eigenschaft in einem anderen Thread auf false festgelegt ist. In der Folge kann das Elapsed-Ereignis ausgelöst werden, nachdem die Enabled-Eigenschaft auf false festgelegt ist. Im Codebeispiel für die Stop-Methode wird eine Möglichkeit gezeigt, diese Racebedingung zu verhindern.

Wenn Enabled auf true und AutoReset auf false festgelegt ist, löst Timer das Elapsed-Ereignis nur einmal nach dem ersten Ablauf des Intervalls aus.

Wenn das Intervall nach dem Start von Timer festgelegt wird, erfolgt ein Zurücksetzen des Zählers. Wenn Sie das Intervall z. B. auf 5 Sekunden festlegen und anschließend die Enabled-Eigenschaft auf true festlegen, beginnt der Zählvorgang zu dem Zeitpunkt, zu dem Enabled festgelegt wird. Wenn Sie das Intervall auf 10 Sekunden zurücksetzen, während der Zähler auf 3 Sekunden steht, wird das Elapsed-Ereignis das erste Mal 13 Sekunden nach dem Festlegen von Enabled auf true ausgelöst.

Hinweis

Einige visuelle Designer, z. B. die Designer in Microsoft Visual Studio, legen die Enabled-Eigenschaft auf true fest, wenn eine neue Timer-Klasse eingefügt wird.

Beispiel

Im folgenden Beispiel wird ein Timer erstellt, der alle 5 Sekunden "Hello World!" in der Konsole anzeigt.

Verwenden Sie für dieses Beispiel den System.Timers-Namespace.

Imports System
Imports System.Timers

Public Class Timer1
    
    Public Shared Sub Main()
        ' Normally, the timer is declared at the class level, so
        ' that it doesn't go out of scope when the method ends.
        ' In this example, the timer is needed only while Main 
        ' is executing. However, KeepAlive must be used at the
        ' end of Main, to prevent the JIT compiler from allowing 
        ' aggressive garbage collection to occur before Main 
        ' ends.
        Dim aTimer As New System.Timers.Timer()

        ' Hook up the Elapsed event for the timer.
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

        ' Set the Interval to 2 seconds (2000 milliseconds).
        aTimer.Interval = 2000
        aTimer.Enabled = True
        
        Console.WriteLine("Press the Enter key to exit the program.")
        Console.ReadLine()

        ' Keep the timer alive until the end of Main.
        GC.KeepAlive(aTimer)
    End Sub
        
    ' Specify what you want to happen when the Elapsed event is 
    ' raised.
    Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
        Console.WriteLine("Hello World!")
    End Sub
End Class
using System;
using System.Timers;

public class Timer1
{
    public static void Main()
    {
        // Normally, the timer is declared at the class level, so
        // that it doesn't go out of scope when the method ends.
        // In this example, the timer is needed only while Main 
        // is executing. However, KeepAlive must be used at the
        // end of Main, to prevent the JIT compiler from allowing 
        // aggressive garbage collection to occur before Main 
        // ends.
        System.Timers.Timer aTimer = new System.Timers.Timer();

        // Hook up the Elapsed event for the timer.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        // Set the Interval to 2 seconds (2000 milliseconds).
        aTimer.Interval = 2000;
        aTimer.Enabled = true;
 
        Console.WriteLine("Press the Enter key to exit the program.");
        Console.ReadLine();

        // Keep the timer alive until the end of Main.
        GC.KeepAlive(aTimer);
    }
 
    // Specify what you want to happen when the Elapsed event is 
    // raised.
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("Hello World!");
    }
}
 
#using <system.dll>

using namespace System;
using namespace System::Timers;

public ref class Timer1
{
public:
   static void Demo()
   {
      // Normally, the timer is declared at the class level, so
      // that it doesn't go out of scope when the method ends.
      // In this example, the timer is needed only while Demo
      // is executing. However, KeepAlive must be used at the
      // end of Demo, to prevent the JIT compiler from allowing 
      // aggressive garbage collection to occur before Demo
      // ends.
      System::Timers::Timer^ aTimer = gcnew System::Timers::Timer;

      // Hook up the Elapsed event for the timer.
      aTimer->Elapsed += gcnew ElapsedEventHandler( Timer1::OnTimedEvent );
      
      // Set the Interval to 2 seconds (2000 milliseconds).
      aTimer->Interval = 2000;
      aTimer->Enabled = true;

      Console::WriteLine("Press the Enter key to exit the program.");
      Console::ReadLine();

      // Keep the timer alive until the end of the Demo method.
      GC::KeepAlive(aTimer);
   }


private:
   // Specify what you want to happen when the Elapsed event is 
   // raised.
   static void OnTimedEvent( Object^ /*source*/, ElapsedEventArgs^ /*e*/ )
   {
      Console::WriteLine( "Hello World!" );
   }

};

int main()
{
   Timer1::Demo();
}

Plattformen

Windows 98, Windows 2000 SP4, Windows Millennium Edition, 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

Siehe auch

Referenz

Timer-Klasse
Timer-Member
System.Timers-Namespace
Interval
Timer.AutoReset-Eigenschaft
Start
Stop
Elapsed