通过


Timer 类

定义

实现一个计时器,该计时器以用户定义的间隔引发事件。 此计时器经过优化,可在 Windows 窗体应用程序中使用,并且必须在窗口中使用。

public ref class Timer : System::ComponentModel::Component
public class Timer : System.ComponentModel.Component
type Timer = class
    inherit Component
Public Class Timer
Inherits Component
继承

示例

以下示例实现一个简单的间隔计时器,该计时器每 5 秒设置一次警报。 当警报发生时,显示 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

注解

A Timer 用于在用户定义的时间间隔引发事件。 此 Windows 计时器专为使用 UI 线程执行处理的单线程环境而设计。 它要求用户代码具有可用的 UI 消息泵,并且始终从同一线程运行,或将调用封送到另一个线程上。

使用此计时器时,请使用 Tick 事件执行轮询操作或显示指定时间段的初始屏幕。 每当属性 Enabled 设置为 true 该属性且 Interval 属性大于零时,将根据 Tick 属性设置按 Interval 间隔引发该事件。

此类提供设置间隔以及启动和停止计时器的方法。

注释

Windows 窗体计时器组件是单线程的,其准确性限制为 55 毫秒。 如果需要具有更高准确度的多线程计时器,请使用 Timer 命名空间中的 System.Timers 类。

构造函数

名称 说明
Timer()

初始化 Timer 类的新实例。

Timer(IContainer)

将类的新实例 Timer 与指定的容器一起初始化。

属性

名称 说明
CanRaiseEvents

获取一个值,该值指示组件是否可以引发事件。

(继承自 Component)
Container

IContainer获取包含 .Component

(继承自 Component)
DesignMode

获取一个值,该值指示当前是否 Component 处于设计模式。

(继承自 Component)
Enabled

获取或设置计时器是否正在运行。

Events

获取附加到此 Component对象的事件处理程序的列表。

(继承自 Component)
Interval

获取或设置相对于事件最后一个匹配Tick项引发事件之前Tick的时间(以毫秒为单位)。

Site

获取或设置 ISite .Component

(继承自 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

在指定的计时器间隔已过且启用计时器时发生。

适用于