Timer.AutoReset Właściwość
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
public:
property bool AutoReset { bool get(); void set(bool value); };
public bool AutoReset { get; set; }
[System.Timers.TimersDescription("TimerAutoReset")]
public bool AutoReset { get; set; }
[System.Timers.TimersDescription("Indicates whether the timer will be restarted when it is enabled.")]
public bool AutoReset { get; set; }
member this.AutoReset : bool with get, set
[<System.Timers.TimersDescription("TimerAutoReset")>]
member this.AutoReset : bool with get, set
[<System.Timers.TimersDescription("Indicates whether the timer will be restarted when it is enabled.")>]
member this.AutoReset : bool with get, set
Public Property AutoReset As Boolean
Wartość właściwości
trueTimer jeśli zdarzenie powinno pojawić Elapsed się za każdym razem, gdy interwał upłynie. false Jeśli zdarzenie powinno pojawić Elapsed się tylko raz, po pierwszym upływie interwału. Wartość domyślna to true.
- Atrybuty
Przykłady
Poniższy przykład tworzy Timer zdarzenie, którego Elapsed zdarzenie jest uruchamiane po 1,5 sekundy. Następnie program obsługi zdarzeń wyświetla komunikat "Hello world!" w konsoli programu .
#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.
Uwagi
Jeśli parametr Timer jest już włączony po wywołaniu Start metody, interwał jest resetowany. Jeśli AutoReset jest to false, należy wywołać metodę Start , aby ponownie uruchomić liczbę.
Zresetowanie interwału ma wpływ na to, kiedy Elapsed zdarzenie zostanie podniesione. Jeśli na przykład ustawisz interwał na 5 sekund, a następnie ustawisz Enabled właściwość na truewartość , liczba rozpoczyna się w czasie Enabled jest ustawiona. Jeśli zresetujesz interwał do 10 sekund, gdy liczba wynosi 3 sekundy, Elapsed zdarzenie zostanie podniesione po raz pierwszy 13 sekund po Enabled ustawieniu właściwości na truewartość .