Aracılığıyla paylaş


Timer.Enabled Özellik

Tanım

öğesinin olayı tetikleyip tetiklemeyeceğini TimerElapsed belirten bir değer alır veya ayarlar.

public:
 property bool Enabled { bool get(); void set(bool value); };
public bool Enabled { get; set; }
[System.Timers.TimersDescription("TimerEnabled")]
public bool Enabled { get; set; }
member this.Enabled : bool with get, set
[<System.Timers.TimersDescription("TimerEnabled")>]
member this.Enabled : bool with get, set
Public Property Enabled As Boolean

Özellik Değeri

true Timer olayı tetiklerseElapsed; aksi takdirde , false. Varsayılan değer: false.

Öznitelikler

Özel durumlar

Zamanlayıcı atıldığından bu özellik ayarlanamıyor.

Özelliği, Interval zamanlayıcı etkinleştirilmeden önce Int32.MaxValue değerinden büyük bir değere ayarlanmıştı.

Örnekler

Aşağıdaki örnek, iki saniyede bir (2000 milisaniye) olayını başlatanTimer.Elapsed, Timer olay için bir olay işleyicisi ayarlayan ve zamanlayıcıyı başlatan bir nesnenin örneğini oluşturur. Olay işleyicisi, her yükseltildiğinde özelliğinin ElapsedEventArgs.SignalTime değerini görüntüler.

using System;
using System.Timers;

public class Example
{
    private static Timer aTimer;

    public static void Main()
    {
        // Create a timer and set a two second interval.
        aTimer = new System.Timers.Timer();
        aTimer.Interval = 2000;

        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;

        // Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = true;

        // Start the timer
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program at any time... ");
        Console.ReadLine();
    }

    private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }
}
// The example displays output like the following: 
//       Press the Enter key to exit the program at any time... 
//       The Elapsed event was raised at 5/20/2015 8:48:58 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:00 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:02 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:04 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:06 PM
open System.Timers

let onTimedEvent source (e: ElapsedEventArgs) =
    printfn $"The Elapsed event was raised at {e.SignalTime}"

// Create a timer and set a two second interval.
let aTimer = new Timer()
aTimer.Interval <- 2000

// Hook up the Elapsed event for the timer. 
aTimer.Elapsed.AddHandler onTimedEvent

// Have the timer fire repeated events (true is the default)
aTimer.AutoReset <- true

// Start the timer
aTimer.Enabled <- true

printfn "Press the Enter key to exit the program at any time... "
stdin.ReadLine() |> ignore

// The example displays output like the following: 
//       Press the Enter key to exit the program at any time... 
//       The Elapsed event was raised at 5/20/2015 8:48:58 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:00 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:02 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:04 PM 
//       The Elapsed event was raised at 5/20/2015 8:49:06 PM
Imports System.Timers

Public Module Example
    Private aTimer As Timer

    Public Sub Main()
        ' Create a timer and set a two second interval.
        aTimer = New System.Timers.Timer()
        aTimer.Interval = 2000

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

        ' Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = True

        ' Start the timer
        aTimer.Enabled = True

        Console.WriteLine("Press the Enter key to exit the program at any time... ")
        Console.ReadLine()
    End Sub

    Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime)
    End Sub
End Module
' The example displays output like the following: 
'       Press the Enter key to exit the program at any time... 
'       The Elapsed event was raised at 5/20/2015 8:48:58 PM 
'       The Elapsed event was raised at 5/20/2015 8:49:00 PM 
'       The Elapsed event was raised at 5/20/2015 8:49:02 PM 
'       The Elapsed event was raised at 5/20/2015 8:49:04 PM 
'       The Elapsed event was raised at 5/20/2015 8:49:06 PM

Açıklamalar

ayarı Enabledtrue , çağrısıyla Startaynıdır, olarak ayar Enabledfalse ise çağrısıyla Stopaynıdır.

Uyarı

Olayı tetikleye sinyal Elapsed her zaman bir ThreadPool iş parçacığında yürütülmek üzere kuyruğa alınır. Bu, özelliği olarak ayarlandıktan falsesonra olayın tetiklenmesine Enabled neden Elapsed olabilir. yönteminin Stop kod örneği, bu yarış koşuluna geçici bir çözüm gösterir.

olarak ayarlanırsa ve olarak ayarlanırsa EnabledTimerfalse, zaman aralığı ilk kez geçtiğinde olayı yalnızca bir kez yükseltirElapsed.AutoResettrue

Aralık başlatıldıktan sonra Timer ayarlanırsa, sayı sıfırlanır. Örneğin, aralığı 5 saniye olarak ayarlar ve ardından özelliğini olarak trueayarlarsanızEnabled, sayı o anda Enabled başlar. Sayım 3 saniye olduğunda aralığı 10 saniyeye sıfırlarsanız, Elapsed olay olarak ayarlandıktan true13 saniye sonra Enabled ilk kez oluşturulur.

Uyarı

Microsoft Visual Studio'dakiler gibi bazı görsel tasarımcılar, yeni Timerbir eklerken özelliğini olarak true ayarlarEnabled.

Şunlara uygulanır

Ayrıca bkz.