Lire en anglais

Partager via


Timer.Enabled Propriété

Définition

Obtient ou définit si la minuterie est en cours d'exécution.

C#
public virtual bool Enabled { get; set; }

Valeur de propriété

Boolean

true si la minuterie est activée ; sinon, false. La valeur par défaut est false.

Exemples

L’exemple suivant implémente un minuteur d’intervalle simple, qui déclenche une alarme toutes les cinq secondes. Lorsque l’alarme se produit, un MessageBox nombre de fois que l’alarme a démarré et invite l’utilisateur à déterminer si le minuteur doit continuer à s’exécuter.

C#
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;
    }
 }

Remarques

Le minuteur n’est pas soumis au garbage collection lorsque la valeur est true.

L’appel de la Start méthode est identique à truela valeur Enabled . De même, l’appel de la Stop méthode est le même que le paramètre Enabled sur false.

S’applique à

Produit Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7