Timer 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
实现按用户定义的时间间隔引发事件的计时器。 此计时器最宜用于 Windows 窗体应用程序中,并且必须在窗口中使用。
public ref class Timer : System::ComponentModel::Componentpublic class Timer : System.ComponentModel.Componenttype Timer = class
    inherit ComponentPublic Class Timer
Inherits Component- 继承
示例
以下示例实现一个简单的间隔计时器,该计时器每五秒发出一次警报。 发生警报时, MessageBox 将显示警报已启动的次数的计数,并提示用户是否应继续运行计时器。
public ref class Class1
{
private:
   static System::Windows::Forms::Timer^ myTimer = gcnew System::Windows::Forms::Timer;
   static int alarmCounter = 1;
   static bool exitFlag = false;
   // This is the method to run when the timer is raised.
   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?", String::Format( "Count is: {0}", 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 void Main()
   {
      
      /* Adds the event and the event handler for the method that will 
                process the timer event to the timer. */
      myTimer->Tick += gcnew EventHandler( TimerEventProcessor );
      
      // Sets the timer interval to 5 seconds.
      myTimer->Interval = 5000;
      myTimer->Start();
      
      // Runs the timer, and raises the event.
      while ( !exitFlag )
      {
         
         // Processes all the events in the queue.
         Application::DoEvents();
      }
   }
};
int main()
{
   Class1::Main();
}
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) {
          // Processes all the events in the queue.
          Application.DoEvents();
       }
    return 0;
    }
 }
Public Class Class1
    Private Shared WithEvents myTimer As New System.Windows.Forms.Timer()
    Private Shared alarmCounter As Integer = 1
    Private Shared exitFlag As Boolean = False    
    
    ' This is the method to run when the timer is raised.
    Private Shared Sub TimerEventProcessor(myObject As Object, _
                                           ByVal myEventArgs As EventArgs) _
                                       Handles myTimer.Tick
        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 Then
            ' Restarts the timer and increments the counter.
            alarmCounter += 1
            myTimer.Enabled = True
        Else
            ' Stops the timer.
            exitFlag = True
        End If
    End Sub
    
    Public Shared Sub Main()
        ' Adds the event and the event handler for the method that will
        ' process the timer event to the timer.
        
        ' 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()
        End While
    End Sub    
End Class
注解
Timer用于以用户定义的间隔引发事件。 此 Windows 计时器专为使用 UI 线程执行处理的单线程环境而设计。 它要求用户代码具有可用的 UI 消息泵,并且始终从同一线程运行,或将调用封送到另一个线程。
使用此计时器时,请使用 Tick 事件执行轮询操作或显示指定时间段的初始屏幕。 每当属性 Enabled 设置为 true 且 Interval 属性大于零时,将根据 Tick 属性设置每隔一段时间 Interval 引发事件。
此类提供用于设置间隔以及启动和停止计时器的方法。
注意
Windows 窗体 Timer 组件是单线程的,精度限制为 55 毫秒。 如果需要更准确的多线程计时器,请使用 Timer 命名空间中的 System.Timers 类。
构造函数
| Timer() | 初始化 Timer 类的新实例。 | 
| Timer(IContainer) | 将指定的容器与 Timer 类的新实例一起初始化。 | 
属性
| CanRaiseEvents | 获取一个指示组件是否可以引发事件的值。(继承自 Component) | 
| Container | 获取包含 IContainer 的 Component。(继承自 Component) | 
| DesignMode | 获取一个值,用以指示 Component 当前是否处于设计模式。(继承自 Component) | 
| Enabled | 获取或设置计时器是否正在运行。 | 
| Events | 获取附加到此 Component 的事件处理程序的列表。(继承自 Component) | 
| Interval | |
| Site | (继承自 Component) | 
| Tag | 获取或设置一个任意字符串,表示某种类型的用户状态。 | 
方法
| CreateObjRef(Type) | 创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。(继承自 MarshalByRefObject) | 
| Dispose() | 释放由 Component 使用的所有资源。(继承自 Component) | 
| Dispose(Boolean) | 释放由计时器使用的资源(内存除外)。 | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetLifetimeService() | 
		已过时.
	 检索控制此实例的生存期策略的当前生存期服务对象。(继承自 MarshalByRefObject) | 
| GetService(Type) | 返回一个对象,该对象表示由 Component 或它的 Container 提供的服务。(继承自 Component) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| InitializeLifetimeService() | 
		已过时.
	 获取生存期服务对象来控制此实例的生存期策略。(继承自 MarshalByRefObject) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| MemberwiseClone(Boolean) | 创建当前 MarshalByRefObject 对象的浅表副本。(继承自 MarshalByRefObject) | 
| OnTick(EventArgs) | 引发 Tick 事件。 | 
| Start() | 启动计时器。 | 
| Stop() | 停止计时器。 | 
| ToString() | 返回表示 Timer 的字符串。 | 
事件
| Disposed | 在通过调用 Dispose() 方法释放组件时发生。(继承自 Component) | 
| Tick | 当指定的计时器间隔已过去而且计时器处于启用状态时发生。 |