Timer, classe
Assembly : System.Windows.Forms (dans system.windows.forms.dll)
Timer sert à déclencher un événement à des intervalles définis par l'utilisateur. Cette minuterie Windows est conçue pour un environnement monothread dans lequel le traitement est effectué à l'aide de threads d'interface utilisateur. Le code de l'utilisateur doit avoir une pompe de messages d'interface utilisateur disponible et il doit toujours opérer à partir du même thread ou marshaler l'appel vers un autre thread.
Avec cette minuterie, utilisez l'événement Tick pour exécuter une interrogation ou afficher un écran de démarrage pendant une durée spécifiée. Chaque fois que la propriété Enabled a la valeur true et que la propriété Interval a une valeur supérieure à zéro, l'événement Tick est déclenché à des intervalles définis par le paramètre de propriété Interval.
Cette classe fournit des méthodes permettant de définir l'intervalle et de démarrer et arrêter la minuterie.
Remarque |
|---|
| Le composant Timer Windows Forms est monothread et limité à une précision de 55 millisecondes. Si vous souhaitez disposer d'une minuterie multithread avec une précision supérieure, utilisez la classe Timer dans l'espace de noms System.Timers. |
L'exemple suivant implémente une minuterie simple qui déclenche une alarme toutes les cinq secondes. Lorsque l'alarme est déclenchée, MessageBox affiche un compteur indiquant le nombre de fois où l'alarme s'est déclenchée et demande à l'utilisateur si la minuterie doit rester active.
public class Class1 { static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer(); static int alarmCounter = 1; static bool exitFlag = false; // This is the method to run when the timer is raised. private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs) { myTimer.Stop(); // Displays a message box asking whether to continue running the timer. if(MessageBox.Show("Continue running?", "Count is: " + alarmCounter, MessageBoxButtons.YesNo) == DialogResult.Yes) { // Restarts the timer and increments the counter. alarmCounter +=1; myTimer.Enabled = true; } else { // Stops the timer. exitFlag = true; } } public static int Main() { /* Adds the event and the event handler for the method that will process the timer event to the timer. */ myTimer.Tick += new EventHandler(TimerEventProcessor); // Sets the timer interval to 5 seconds. myTimer.Interval = 5000; myTimer.Start(); // Runs the timer, and raises the event. while(exitFlag == false) { // Processes all the events in the queue. Application.DoEvents(); } return 0; } }
public class Class1
{
private static System.Windows.Forms.Timer myTimer =
new System.Windows.Forms.Timer();
private static int alarmCounter = 1;
private static boolean exitFlag = false;
// This is the method to run when the timer is raised.
private static void TimerEventProcessor(Object myObject,
EventArgs myEventArgs)
{
myTimer.Stop();
// Displays a message box asking whether to continue running the timer.
if (MessageBox.Show("Continue running?", "Count is: "
+ alarmCounter, MessageBoxButtons.YesNo).Equals(DialogResult.Yes)) {
// Restarts the timer and increments the counter.
alarmCounter += 1;
myTimer.set_Enabled(true);
}
else {
// Stops the timer.
exitFlag = true;
}
} //TimerEventProcessor
public static void main(String[] args)
{
/* Adds the event and the event handler for the method that will
process the timer event to the timer.
*/
myTimer.add_Tick(new EventHandler(TimerEventProcessor));
// Sets the timer interval to 5 seconds.
myTimer.set_Interval(5000);
myTimer.Start();
// Runs the timer, and raises the event.
while (exitFlag == false) {
// Processes all the events in the queue.
Application.DoEvents();
}
return;
} //main
} //Class1
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Remarque