Exportar (0) Imprimir
Expandir Tudo
Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.

Timer.Enabled Propriedade

Gets or sets a value indicating whether the Timer should raise the Elapsed event.

Namespace:  System.Timers
Assembly:  System (em System. dll)

public bool Enabled { get; set; }

Valor da propriedade

Tipo: System.Boolean
true if the Timer should raise the Elapsed event; otherwise, false. O padrão é false.


Esta propriedade não pode ser definida porque o timer foi descartado.

Setting Enabled to true is the same as calling Start, while setting Enabled to false is the same as calling Stop.


The Elapsed event is raised on a ThreadPool thread, so the event-handling method might run on one thread at the same time that the Enabled property is set to false on another thread.This might result in the Elapsed event being raised after the Enabled property is set to false.The code example for the Stop method shows one way to prevent this race condition.

If Enabled is set to true and AutoReset is set to false, the Timer raises the Elapsed event only once, the first time the interval elapses.

Se o intervalo for definido após Timer ser iniciado, a contagem é redefinida. For example, if you set the interval to 5 seconds and then set the Enabled property to true, the count starts at the time Enabled is set.If you reset the interval to 10 seconds when count is 3 seconds, the Elapsed event is raised for the first time 13 seconds after Enabled was set to true.


Some visual designers, such as those in Microsoft Visual Studio, set the Enabled property to true when inserting a new Timer.

The following example creates a Timer that displays "Hello World!" on the console every five seconds.

Use the System.Timers namespace for this example.

using System;
using System.Timers;

public class Timer1
    public static void Main()
        // Normally, the timer is declared at the class level, so
        // that it doesn't go out of scope when the method ends.
        // In this example, the timer is needed only while Main 
        // is executing. However, KeepAlive must be used at the
        // end of Main, to prevent the JIT compiler from allowing 
        // aggressive garbage collection to occur before Main 
        // ends.
        System.Timers.Timer aTimer = new System.Timers.Timer();

        // Hook up the Elapsed event for the timer.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        // Set the Interval to 2 seconds (2000 milliseconds).
        aTimer.Interval = 2000;
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program.");

        // Keep the timer alive until the end of Main.

    // Specify what you want to happen when the Elapsed event is 
    // raised.
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
        Console.WriteLine("Hello World!");

Contribuições da comunidade

© 2014 Microsoft