请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本
System.Timers
Timer 类
Timer 属性
 Enabled 属性

  开启低带宽视图
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
Timer.Enabled 属性

获取或设置一个值,该值指示 Timer 是否应引发 Elapsed 事件。

命名空间:System.Timers
程序集:System(在 system.dll 中)

Visual Basic(声明)
Public Property Enabled As Boolean
Visual Basic(用法)
Dim instance As Timer
Dim value As Boolean

value = instance.Enabled

instance.Enabled = value
C#
public bool Enabled { get; set; }
C++
public:
property bool Enabled {
    bool get ();
    void set (bool value);
}
J#
/** @property */
public boolean get_Enabled ()

/** @property */
public void set_Enabled (boolean value)
JScript
public function get Enabled () : boolean

public function set Enabled (value : boolean)

属性值

如果 Timer 应引发 Elapsed 事件,则为 true;否则,为 false。默认为 false

Enabled 设置为 true 与调用 Start 相同,而将 Enabled 设置为 false 则与调用 Stop 相同。

Note注意

Elapsed 事件在 ThreadPool 线程上引发,因此,在将一个线程上的 Enabled 属性设置为 false 的同时,可在另一个线程上运行事件处理方法。这可能导致在将 Enabled 属性设置为 false 后引发 Elapsed 事件。Stop 方法的代码示例演示了一种防止此争用条件的方法。

如果将 Enabled 设置为 true 并将 AutoReset 设置为 false,则 Timer 在第一次达到间隔时仅引发一次 Elapsed 事件。

如果在 Timer 已启动后设置间隔,计数会重置。例如,如果将间隔设置为 5 秒,然后将 Enabled 属性设置为 true,则计数将在设置 Enabled 时开始。如果在计数为 3 秒时将间隔重置为 10 秒,则 Elapsed 事件在 Enabled 设置为 true 的 13 秒之后第一次引发。

Note注意

一些可视化设计器(如 Microsoft Visual Studio 中的那些)在插入新的 Timer 时,将 Enabled 属性设置为 true

下面的示例创建一个 Timer,它每隔五秒钟在控制台上显示一次“Hello World!”。

对于此示例,使用 System.Timers 命名空间。

Visual Basic
Imports System
Imports System.Timers

Public Class Timer1
    
    Public Shared Sub 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.
        Dim aTimer As New System.Timers.Timer()

        ' Hook up the Elapsed event for the timer.
        AddHandler aTimer.Elapsed, AddressOf 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.")
        Console.ReadLine()

        ' Keep the timer alive until the end of Main.
        GC.KeepAlive(aTimer)
    End Sub
        
    ' Specify what you want to happen when the Elapsed event is 
    ' raised.
    Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
        Console.WriteLine("Hello World!")
    End Sub
End Class

C#
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.");
        Console.ReadLine();

        // Keep the timer alive until the end of Main.
        GC.KeepAlive(aTimer);
    }
 
    // Specify what you want to happen when the Elapsed event is 
    // raised.
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("Hello World!");
    }
}
 
C++
#using <system.dll>

using namespace System;
using namespace System::Timers;

public ref class Timer1
{
public:
   static void Demo()
   {
      // 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 Demo
      // is executing. However, KeepAlive must be used at the
      // end of Demo, to prevent the JIT compiler from allowing 
      // aggressive garbage collection to occur before Demo
      // ends.
      System::Timers::Timer^ aTimer = gcnew System::Timers::Timer;

      // Hook up the Elapsed event for the timer.
      aTimer->Elapsed += gcnew ElapsedEventHandler( Timer1::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.");
      Console::ReadLine();

      // Keep the timer alive until the end of the Demo method.
      GC::KeepAlive(aTimer);
   }


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

};

int main()
{
   Timer1::Demo();
}

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0、1.1、1.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利  |  商标  |  隐私权声明
Page view tracker