Timer.Interval 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.
Obtient ou définit l'intervalle, exprimé en millisecondes, auquel l'événement Elapsed doit être déclenché.
public:
property double Interval { double get(); void set(double value); };
public double Interval { get; set; }
[System.Timers.TimersDescription("TimerInterval")]
public double Interval { get; set; }
[System.Timers.TimersDescription("TimerInterval")]
[System.ComponentModel.SettingsBindable(true)]
public double Interval { get; set; }
member this.Interval : double with get, set
[<System.Timers.TimersDescription("TimerInterval")>]
member this.Interval : double with get, set
[<System.Timers.TimersDescription("TimerInterval")>]
[<System.ComponentModel.SettingsBindable(true)>]
member this.Interval : double with get, set
Public Property Interval As Double
Valeur de propriété
Intervalle en millisecondes séparant les événements Elapsed. La valeur doit être supérieure à zéro et inférieure ou égale à Int32.MaxValue. La valeur par défaut est 100 millisecondes.
- Attributs
Exceptions
L'intervalle est inférieur ou égal à zéro.
- ou -
L’intervalle est supérieur à Int32.MaxValue, et le minuteur est actuellement activé. (Si la minuterie n'est pas activée actuellement, aucune exception n'est levée jusqu'à ce qu'elle soit activée.)
Exemples
L’exemple suivant instancie un Timer objet qui déclenche son Timer.Elapsed événement toutes les deux secondes (2 000 millisecondes), configure un gestionnaire d’événements pour l’événement et démarre le minuteur. Le gestionnaire d’événements affiche la valeur de la ElapsedEventArgs.SignalTime propriété chaque fois qu’elle est déclenchée.
using namespace System;
using namespace System::Timers;
public ref class Example
{
private:
static System::Timers::Timer^ aTimer;
public:
static void Demo()
{
// Create a timer and set a two second interval.
aTimer = gcnew System::Timers::Timer();
aTimer->Interval = 2000;
// Hook up the Elapsed event for the timer.
aTimer->Elapsed += gcnew System::Timers::ElapsedEventHandler(Example::OnTimedEvent);
// Have the timer fire repeated events (true is the default)
aTimer->AutoReset = true;
// Start the timer
aTimer->Enabled = true;
Console::WriteLine("Press the Enter key to exit the program at any time... ");
Console::ReadLine();
}
private:
static void OnTimedEvent(Object^ source, System::Timers::ElapsedEventArgs^ e)
{
Console::WriteLine("The Elapsed event was raised at {0}", e->SignalTime);
}
};
int main()
{
Example::Demo();
}
// The example displays output like the following:
// Press the Enter key to exit the program at any time...
// The Elapsed event was raised at 5/20/2015 8:48:58 PM
// The Elapsed event was raised at 5/20/2015 8:49:00 PM
// The Elapsed event was raised at 5/20/2015 8:49:02 PM
// The Elapsed event was raised at 5/20/2015 8:49:04 PM
// The Elapsed event was raised at 5/20/2015 8:49:06 PM
using System;
using System.Timers;
public class Example
{
private static Timer aTimer;
public static void Main()
{
// Create a timer and set a two second interval.
aTimer = new System.Timers.Timer();
aTimer.Interval = 2000;
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += OnTimedEvent;
// Have the timer fire repeated events (true is the default)
aTimer.AutoReset = true;
// Start the timer
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program at any time... ");
Console.ReadLine();
}
private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
}
// The example displays output like the following:
// Press the Enter key to exit the program at any time...
// The Elapsed event was raised at 5/20/2015 8:48:58 PM
// The Elapsed event was raised at 5/20/2015 8:49:00 PM
// The Elapsed event was raised at 5/20/2015 8:49:02 PM
// The Elapsed event was raised at 5/20/2015 8:49:04 PM
// The Elapsed event was raised at 5/20/2015 8:49:06 PM
open System.Timers
let onTimedEvent source (e: ElapsedEventArgs) =
printfn $"The Elapsed event was raised at {e.SignalTime}"
// Create a timer and set a two second interval.
let aTimer = new Timer()
aTimer.Interval <- 2000
// Hook up the Elapsed event for the timer.
aTimer.Elapsed.AddHandler onTimedEvent
// Have the timer fire repeated events (true is the default)
aTimer.AutoReset <- true
// Start the timer
aTimer.Enabled <- true
printfn "Press the Enter key to exit the program at any time... "
stdin.ReadLine() |> ignore
// The example displays output like the following:
// Press the Enter key to exit the program at any time...
// The Elapsed event was raised at 5/20/2015 8:48:58 PM
// The Elapsed event was raised at 5/20/2015 8:49:00 PM
// The Elapsed event was raised at 5/20/2015 8:49:02 PM
// The Elapsed event was raised at 5/20/2015 8:49:04 PM
// The Elapsed event was raised at 5/20/2015 8:49:06 PM
Imports System.Timers
Public Module Example
Private aTimer As Timer
Public Sub Main()
' Create a timer and set a two second interval.
aTimer = New System.Timers.Timer()
aTimer.Interval = 2000
' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Have the timer fire repeated events (true is the default)
aTimer.AutoReset = True
' Start the timer
aTimer.Enabled = True
Console.WriteLine("Press the Enter key to exit the program at any time... ")
Console.ReadLine()
End Sub
Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime)
End Sub
End Module
' The example displays output like the following:
' Press the Enter key to exit the program at any time...
' The Elapsed event was raised at 5/20/2015 8:48:58 PM
' The Elapsed event was raised at 5/20/2015 8:49:00 PM
' The Elapsed event was raised at 5/20/2015 8:49:02 PM
' The Elapsed event was raised at 5/20/2015 8:49:04 PM
' The Elapsed event was raised at 5/20/2015 8:49:06 PM
Remarques
Vous utilisez la Interval propriété pour déterminer la fréquence à laquelle l’événement Elapsed est déclenché. Étant donné que la Timer classe dépend de l’horloge système, elle a la même résolution que l’horloge système. Cela signifie que l’événement Elapsed se déclenche à un intervalle défini par la résolution de l’horloge système si la Interval propriété est inférieure à la résolution de l’horloge système. L’exemple suivant définit la Interval propriété sur 5 millisecondes. Lorsqu’il est exécuté sur un système Windows dont l’horloge système a une résolution d’environ 15 millisecondes, l’événement se déclenche toutes les 15 millisecondes plutôt que toutes les 5 millisecondes.
Notes
L’horloge système utilisée est la même horloge que celle utilisée par GetTickCount, qui n’est pas affectée par les modifications apportées avec timeBeginPeriod et timeEndPeriod.
using System;
using System.IO;
using System.Collections.Generic;
using System.Timers;
public class Example
{
private static Timer aTimer;
private static List<String> eventlog;
private static int nEventsFired = 0;
private static DateTime previousTime;
public static void Main()
{
eventlog = new List<String>();
StreamWriter sr = new StreamWriter(@".\Interval.txt");
// Create a timer with a five millisecond interval.
aTimer = new Timer(5);
aTimer.Elapsed += OnTimedEvent;
// Hook up the Elapsed event for the timer.
aTimer.AutoReset = true;
sr.WriteLine("The timer should fire every {0} milliseconds.",
aTimer.Interval);
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program... ");
Console.ReadLine();
foreach (var item in eventlog)
sr.WriteLine(item);
sr.Close();
Console.WriteLine("Terminating the application...");
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
eventlog.Add(String.Format("Elapsed event at {0:HH':'mm':'ss.ffffff} ({1})",
e.SignalTime,
nEventsFired++ == 0 ?
0.0 : (e.SignalTime - previousTime).TotalMilliseconds));
previousTime = e.SignalTime;
if (nEventsFired == 20) {
Console.WriteLine("No more events will fire...");
aTimer.Enabled = false;
}
}
}
// The example writes output like the following to a file:
// The timer should fire every 5 milliseconds.
// Elapsed event at 08:42:49.370344 (0)
// Elapsed event at 08:42:49.385345 (15.0015)
// Elapsed event at 08:42:49.400347 (15.0015)
// Elapsed event at 08:42:49.415348 (15.0015)
// Elapsed event at 08:42:49.430350 (15.0015)
// Elapsed event at 08:42:49.445351 (15.0015)
// Elapsed event at 08:42:49.465353 (20.002)
// Elapsed event at 08:42:49.480355 (15.0015)
// Elapsed event at 08:42:49.495356 (15.0015)
// Elapsed event at 08:42:49.510358 (15.0015)
// Elapsed event at 08:42:49.525359 (15.0015)
// Elapsed event at 08:42:49.540361 (15.0015)
// Elapsed event at 08:42:49.555362 (15.0015)
// Elapsed event at 08:42:49.570364 (15.0015)
// Elapsed event at 08:42:49.585365 (15.0015)
// Elapsed event at 08:42:49.605367 (20.002)
// Elapsed event at 08:42:49.620369 (15.0015)
// Elapsed event at 08:42:49.635370 (15.0015)
// Elapsed event at 08:42:49.650372 (15.0015)
// Elapsed event at 08:42:49.665373 (15.0015)
open System
open System.IO
open System.Timers
let aTimer = new Timer 5
let eventlog = ResizeArray()
let mutable nEventsFired = 0
let mutable previousTime = DateTime()
let onTimedEvent source (e: ElapsedEventArgs) =
String.Format("Elapsed event at {0:HH':'mm':'ss.ffffff} ({1})", e.SignalTime, if nEventsFired = 0 then 0. else (e.SignalTime - previousTime).TotalMilliseconds)
|> eventlog.Add
nEventsFired <- nEventsFired + 1
previousTime <- e.SignalTime
if nEventsFired = 20 then
printfn "No more events will fire..."
aTimer.Enabled <- false
[<EntryPoint>]
let main _ =
use sr = new StreamWriter(@".\Interval.txt")
// Create a timer with a five millisecond interval.
aTimer.Elapsed.AddHandler onTimedEvent
// Hook up the Elapsed event for the timer.
aTimer.AutoReset <- true
sr.WriteLine $"The timer should fire every {aTimer.Interval} milliseconds."
aTimer.Enabled <- true
printfn "Press the Enter key to exit the program... "
stdin.ReadLine() |> ignore
for item in eventlog do
sr.WriteLine item
printfn "Terminating the application..."
0
// The example writes output like the following to a file:
// The timer should fire every 5 milliseconds.
// Elapsed event at 08:42:49.370344 (0)
// Elapsed event at 08:42:49.385345 (15.0015)
// Elapsed event at 08:42:49.400347 (15.0015)
// Elapsed event at 08:42:49.415348 (15.0015)
// Elapsed event at 08:42:49.430350 (15.0015)
// Elapsed event at 08:42:49.445351 (15.0015)
// Elapsed event at 08:42:49.465353 (20.002)
// Elapsed event at 08:42:49.480355 (15.0015)
// Elapsed event at 08:42:49.495356 (15.0015)
// Elapsed event at 08:42:49.510358 (15.0015)
// Elapsed event at 08:42:49.525359 (15.0015)
// Elapsed event at 08:42:49.540361 (15.0015)
// Elapsed event at 08:42:49.555362 (15.0015)
// Elapsed event at 08:42:49.570364 (15.0015)
// Elapsed event at 08:42:49.585365 (15.0015)
// Elapsed event at 08:42:49.605367 (20.002)
// Elapsed event at 08:42:49.620369 (15.0015)
// Elapsed event at 08:42:49.635370 (15.0015)
// Elapsed event at 08:42:49.650372 (15.0015)
// Elapsed event at 08:42:49.665373 (15.0015)
Imports System.Collections.Generic
Imports System.IO
Imports System.Timers
Module Example
Private WithEvents aTimer As Timer
Private eventlog As List(Of String)
Private nEventsFired As Integer = 0
Private previousTime As Date
Public Sub Main()
eventlog = New List(Of String)()
Dim sr As New StreamWriter(".\Interval.txt")
' Create a timer with a five millisecond interval.
aTimer = New Timer(5)
aTimer.AutoReset = True
sr.WriteLine("The timer should fire every {0} milliseconds.",
aTimer.Interval)
aTimer.Enabled = True
Console.WriteLine("Press the Enter key to exit the program... ")
Console.ReadLine()
For Each item In eventlog
sr.WriteLine(item)
Next
sr.Close()
Console.WriteLine("Terminating the application...")
End Sub
Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs) _
Handles aTimer.Elapsed
eventlog.Add(String.Format("Elapsed event at {0:HH':'mm':'ss.ffffff} ({1})",
e.SignalTime,
If(nEventsFired = 0,
0.0, (e.SignalTime - previousTime).TotalMilliseconds)))
nEventsFired += 1
previousTime = e.SignalTime
if nEventsFired = 20 Then
Console.WriteLine("No more events will fire...")
aTimer.Enabled = False
End If
End Sub
End Module
' The example displays the following output:
' The timer should fire every 5 milliseconds.
' Elapsed event at 08:42:49.370344 (0)
' Elapsed event at 08:42:49.385345 (15.0015)
' Elapsed event at 08:42:49.400347 (15.0015)
' Elapsed event at 08:42:49.415348 (15.0015)
' Elapsed event at 08:42:49.430350 (15.0015)
' Elapsed event at 08:42:49.445351 (15.0015)
' Elapsed event at 08:42:49.465353 (20.002)
' Elapsed event at 08:42:49.480355 (15.0015)
' Elapsed event at 08:42:49.495356 (15.0015)
' Elapsed event at 08:42:49.510358 (15.0015)
' Elapsed event at 08:42:49.525359 (15.0015)
' Elapsed event at 08:42:49.540361 (15.0015)
' Elapsed event at 08:42:49.555362 (15.0015)
' Elapsed event at 08:42:49.570364 (15.0015)
' Elapsed event at 08:42:49.585365 (15.0015)
' Elapsed event at 08:42:49.605367 (20.002)
' Elapsed event at 08:42:49.620369 (15.0015)
' Elapsed event at 08:42:49.635370 (15.0015)
' Elapsed event at 08:42:49.650372 (15.0015)
' Elapsed event at 08:42:49.665373 (15.0015)
Si votre application nécessite une résolution supérieure à celle offerte par la Timer classe ou l’horloge système, utilisez les minuteurs multimédias haute résolution. Consultez Guide pratique pour utiliser le minuteur High-Resolution.
Si l’intervalle est défini après le Timer démarrage, le nombre est réinitialisé. Par exemple, si vous définissez l’intervalle sur 5 secondes, puis que vous définissez la Enabled propriété sur true
, le nombre démarre au moment Enabled où est défini. Si vous réinitialisez l’intervalle à 10 secondes quand le nombre est de 3 secondes, l’événement Elapsed est déclenché pour la première fois 13 secondes après Enabled avoir été défini sur true
.
Si Enabled a la true
valeur et AutoReset a la valeur false
, le Timer déclenche l’événement Elapsed une seule fois, la première fois que l’intervalle s’écoule.
Enabled est ensuite défini sur false
.
Notes
Si Enabled et AutoReset sont tous deux définis sur false
, et que le minuteur a été activé précédemment, la définition de la Interval propriété entraîne le Elapsed déclenche l’événement une fois, comme si la Enabled propriété avait été définie sur true
. Pour définir l’intervalle sans déclencher l’événement, vous pouvez définir temporairement la propriété sur true
, définir la Interval propriété sur l’intervalle de temps souhaité, puis définir immédiatement la Enabled propriété sur false
.Enabled