Timer.Enabled Timer.Enabled Timer.Enabled Timer.Enabled Property

定義

TimerElapsed イベントを発生させる必要があるかどうかを示す値を取得または設定します。Gets or sets a value indicating whether the Timer should raise the Elapsed event.

public:
 property bool Enabled { bool get(); void set(bool value); };
[System.Timers.TimersDescription("Indicates whether the timer is enabled to fire events at a defined interval.")]
[System.Timers.TimersDescription("TimerEnabled")]
public bool Enabled { get; set; }
member this.Enabled : bool with get, set
Public Property Enabled As Boolean

プロパティ値

TimerElapsed イベントを発生させる場合は true。それ以外の場合は falsetrue if the Timer should raise the Elapsed event; otherwise, false. 既定値は、false です。The default is false.

例外

タイマーが破棄されたため、このプロパティは設定できません。This property cannot be set because the timer has been disposed.

Interval プロパティは、タイマーが有効になる前に MaxValue より大きい値に設定されました。The Interval property was set to a value greater than MaxValue before the timer was enabled.

次の例ではTimer 、2秒 ( Timer.Elapsed 2000 ミリ秒) ごとにイベントを発生させるオブジェクトをインスタンス化し、イベントのイベントハンドラーを設定して、タイマーを開始します。The following example instantiates a Timer object that fires its Timer.Elapsed event every two seconds (2000 milliseconds), sets up an event handler for the event, and starts the timer. イベントハンドラーは、発生するたびにElapsedEventArgs.SignalTimeプロパティの値を表示します。The event handler displays the value of the ElapsedEventArgs.SignalTime property each time it is raised.

using namespace System;
using namespace System::Timers;

public ref class Example
{
private:
    static System::Timers::Timer^ aTimer;

public:
    static void Demo()
    {
        // Create a timer and set a two second interval.
        aTimer = gcnew System::Timers::Timer();
        aTimer->Interval = 2000;

        // Hook up the Elapsed event for the timer. 
        aTimer->Elapsed += gcnew System::Timers::ElapsedEventHandler(Example::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);
    }
};

int main()
{
    Example::Demo();
}
// 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 

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 
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 

注釈

Start Enabled false Stopをに設定することはと同じですが、をに設定することは、を呼び出す場合と同じです。true EnabledSetting Enabled to true is the same as calling Start, while setting Enabled to false is the same as calling Stop.

注意

Elapsedイベントを発生させるシグナルは、常にThreadPoolスレッドで実行するためにキューに入れられます。The signal to raise the Elapsed event is always queued for execution on a ThreadPool thread. これにより、 Elapsed Enabledプロパティがに設定された後にイベントfalseが発生する可能性があります。This might result in the Elapsed event being raised after the Enabled property is set to false. コード例をStopメソッドは、この競合状態を回避する 1 つのメソッドを示しています。The code example for the Stop method shows one way to work around this race condition.

Enabled false Elapsed Timerに設定trueされ、がに設定されている場合、は、最初の間隔が経過したときに1回だけイベントを発生AutoResetさせます。If Enabled is set to true and AutoReset is set to false, the Timer raises the Elapsed event only once, the first time the interval elapses.

Timerが開始した後に間隔が設定されている場合、カウントはリセットされます。If the interval is set after the Timer has started, the count is reset. たとえば、間隔を5秒に設定し、 Enabledプロパティをにtrue設定すると、 Enabledが設定された時点でカウントが開始されます。For example, if you set the interval to 5 seconds and then set the Enabled property to true, the count starts at the time Enabled is set. [カウント] Elapsedを3秒に設定した場合、間隔を10秒にリセットすると、がにtrue設定さEnabledれてから13秒後にイベントが発生します。If you reset the interval to 10 seconds when count is 3 seconds, the Elapsed event is raised for the first time 13 seconds after Enabled was set to true.

注意

Microsoft Visual Studio のようなビジュアルデザイナーの中には、新しいEnabled Timerを挿入trueするときにプロパティをに設定するものがあります。Some visual designers, such as those in Microsoft Visual Studio, set the Enabled property to true when inserting a new Timer.

適用対象

こちらもご覧ください