Timer.AutoReset Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
public:
property bool AutoReset { bool get(); void set(bool value); };
public bool AutoReset { get; set; }
[System.Timers.TimersDescription("TimerAutoReset")]
public bool AutoReset { get; set; }
member this.AutoReset : bool with get, set
[<System.Timers.TimersDescription("TimerAutoReset")>]
member this.AutoReset : bool with get, set
Public Property AutoReset As Boolean
Valore della proprietà
true
se Timer deve generare l'evento Elapsed allo scadere di ogni intervallo; false
se deve generare l'evento Elapsed solo una volta, in corrispondenza della prima scadenza dell'intervallo. Il valore predefinito è true
.
- Attributi
Esempio
Nell'esempio seguente viene creato un oggetto Timer il cui Elapsed evento viene generato dopo 1,5 secondi. Il gestore eventi visualizza quindi "Hello World!" nella 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.
open System.Threading
open System.Timers
// Handle the Elapsed event.
let onTimedEvent source e =
printfn "Hello World!"
// Create a timer with a 1.5 second interval.
let interval = 1500.
let aTimer = new Timer(interval)
// Hook up the event handler for the Elapsed event.
aTimer.Elapsed.AddHandler(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.
Thread.Sleep(interval * 2. |> int)
printfn "Press the Enter key to exit the program."
stdin.ReadLine() |> ignore
// 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.
Commenti
Se AutoReset è false
, il Start metodo deve essere chiamato per avviare nuovamente il conteggio.
La reimpostazione dell'intervallo influisce sulla generazione dell'evento Elapsed . Ad esempio, se si imposta l'intervallo su 5 secondi e quindi si imposta la Enabled proprietà true
su , il conteggio inizia al momento Enabled dell'impostazione. Se si reimposta l'intervallo su 10 secondi quando il conteggio è di 3 secondi, l'evento Elapsed viene generato per la prima volta 13 secondi dopo che la Enabled proprietà è stata impostata su true
.