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

정의

Timer에서 Elapsed 이벤트를 한 번만(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

속성 값

간격이 경과할 때마다 Timer에서 Elapsed 이벤트를 발생시켜야 하면 true이고, 첫 번째 간격이 경과한 후 한 번만 Elapsed 이벤트를 발생시켜야 하면 false입니다.true 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.

예제

다음 예제는 Timer 해당 Elapsed 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.

설명

경우는 Timer 이미 때 사용할 수는 Start 메서드가 호출 되 면 재설정 됩니다.If the Timer is already enabled when the Start method is called, the interval is reset. 하는 경우 AutoReset 됩니다 false, Start 개수를 다시 시작 하기 위해 메서드를 호출 해야 합니다.If 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 초, 10 초 간격을 재설정 하는 경우는 Elapsed 13 초 후 처음으로 이벤트가 발생 합니다 Enabled 속성이로 설정 된 true합니다.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.

적용 대상

추가 정보