Timer.AutoReset Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
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
Valeur de propriété
true si l’événement Timer doit déclencher l’événement Elapsed chaque fois que l’intervalle s’est écoulé ; false s’il ne doit déclencher l’événement Elapsed qu’une seule fois, après la première fois que l’intervalle s’est écoulé. La valeur par défaut est true.
- Attributs
Exemples
L’exemple suivant crée un Timer événement dont Elapsed l’événement se déclenche après 1,5 seconde. Son gestionnaire d’événements affiche ensuite « Hello World ! » sur la console.
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.
Remarques
Si AutoReset c’est falsele cas, la Start méthode doit être appelée pour recommencer le nombre.
La réinitialisation de l’intervalle affecte le moment où l’événement Elapsed est déclenché. Par exemple, si vous définissez l’intervalle sur 5 secondes, puis définissez la Enabled propriété truesur , le nombre commence au moment Enabled de la définition. Si vous réinitialisez l’intervalle à 10 secondes lorsque le nombre est de 3 secondes, l’événement Elapsed est déclenché pour la première fois 13 secondes après la définition de la Enabled propriété true.