Timer.AutoReset Timer.AutoReset Timer.AutoReset Timer.AutoReset Property

定義

TimerElapsed イベントを一度のみ発生させる (false) か、または繰り返し発生させる (true) かを示すブール値を取得または設定します。Gets or sets a Boolean indicating whether the Timer should raise the Elapsed event only once (false) or repeatedly (true).

public:
 property bool AutoReset { bool get(); void set(bool value); };
[System.Timers.TimersDescription("Indicates whether the timer will be restarted when it is enabled.")]
[System.Timers.TimersDescription("TimerAutoReset")]
public bool AutoReset { get; set; }
member this.AutoReset : bool with get, set
Public Property AutoReset As Boolean

プロパティ値

指定した間隔が経過するたびに TimerElapsed イベントを発生させる場合は true。最初の間隔が経過した後に 1 回だけ false イベントを発生させる場合は Elapsedtrue if the Timer should raise the Elapsed event each time the interval elapses; false if it should raise the Elapsed event only once, after the first time the interval elapses. 既定値は、true です。The default is true.

次の例ではTimerElapsed 1.5 秒後にイベントを発生させるを作成します。The following example creates a Timer whose Elapsed event fires after 1.5 seconds. そのイベントハンドラーに "Hello World!" と表示されます。Its event handler then displays "Hello World!" コンソールで。on the console.

#using <system.dll>

using namespace System;
using namespace System::Timers;

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

public:
   static void Main()
   {
      // Create a new Timer with Interval set to 1.5 seconds.
      double interval = 1500.0;
      aTimer = gcnew System::Timers::Timer(interval);

      // Hook up the event handler for the Elapsed event.
      aTimer->Elapsed += gcnew ElapsedEventHandler( OnTimedEvent );
      
      // Only raise the event the first time Interval elapses.
      aTimer->AutoReset = false;
      aTimer->Enabled = true;

      // Ensure the event fires before the exit message appears.
      System::Threading::Thread::Sleep((int) interval * 2);
      Console::WriteLine("Press the Enter key to exit the program.");
      Console::ReadLine();

      // If the timer is declared in a long-running method, use
      // KeepAlive to prevent garbage collection from occurring
      // before the method ends.
      //GC::KeepAlive(aTimer);
   }

private:
   // Handle the Elapsed event.
   static void OnTimedEvent( Object^ /*source*/, ElapsedEventArgs^ /*e*/ )
   {
      Console::WriteLine( "Hello World!" );
   }

};

int main()
{
   Timer2::Main();
}
// The example displays the following output:
//       Hello World!
//       Press the Enter key to exit the program.
using System;
using System.Timers;

public class Example
{
    private static Timer aTimer;

    public static void Main()
    {
        // Create a timer with a 1.5 second interval.
        double interval = 1500.0;
        aTimer = new System.Timers.Timer(interval);
        
        // Hook up the event handler for the Elapsed event.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        // Only raise the event the first time Interval elapses.
        aTimer.AutoReset = false;
        aTimer.Enabled = true;
        
        // Ensure the event fires before the exit message appears.
        System.Threading.Thread.Sleep((int) interval * 2);
        Console.WriteLine("Press the Enter key to exit the program.");
        Console.ReadLine();
    }
 
    // Handle the Elapsed event.
    private static void OnTimedEvent(object source, ElapsedEventArgs e) 
    {
        Console.WriteLine("Hello World!");
    }
}
// This example displays the following output:
//       Hello World!
//       Press the Enter key to exit the program.
Imports System.Timers

Public Module Example
    Private aTimer As System.Timers.Timer

    Public Sub Main()
        ' Create a timer with a 1.5 second interval.
        Dim interval As Double = 1500.0
        aTimer = New System.Timers.Timer(interval)

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

        ' Only raise the event the first time Interval elapses.
        aTimer.AutoReset = False
        aTimer.Enabled = True
        
        
        ' Ensure the event fires before the exit message appears.
        System.Threading.Thread.Sleep(CInt(interval * 2))
        Console.WriteLine("Press the Enter key to exit the program.")
        Console.ReadLine()

        ' If the timer is declared in a long-running method, use
        ' KeepAlive to prevent garbage collection from occurring
        ' before the method ends.
        'GC.KeepAlive(aTimer)
    End Sub

    ' Specify what you want to happen when the Elapsed event is 
    ' raised.
    Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
        Console.WriteLine("Hello World!")
    End Sub
End Module
' This example displays the following output:
'       Hello World!
'       Press the Enter key to exit the program.

注釈

Start Timerメソッドが呼び出されたときにが既に有効になっている場合、間隔はリセットされます。If the Timer is already enabled when the Start method is called, the interval is reset. AutoReset Startの場合は、カウントを再び開始するためにメソッドを呼び出す必要があります。 falseIf AutoReset is false, the Start method must be called in order to start the count again.

間隔をリセットすると、 Elapsedイベントが発生したときに影響を及ぼします。Resetting the interval affects when the Elapsed event is raised. たとえば、間隔を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. カウントが3秒Elapsedのときに間隔を10秒にリセットすると、 Enabledプロパティがにtrue設定された後、最初の13秒間イベントが発生します。If you reset the interval to 10 seconds when the count is 3 seconds, the Elapsed event is raised for the first time 13 seconds after the Enabled property was set to true.

適用対象

こちらもご覧ください