如题,求详解,最好是能有个例子说明.

解决方案 »

  1.   

    下面的代码示例为 Timer..::.Elapsed 事件设置一个事件处理程序、创建计时器并启动该计时器。该事件处理程序每次引发时都会显示 SignalTime 属性。Visual Basic  复制代码 
    Imports System
    Imports System.TimersPublic Class Timer1    Private Shared aTimer As System.Timers.Timer    Public Shared Sub Main()
            ' Normally, the timer is declared at the class level,
            ' so that it stays in scope as long as it is needed.
            ' If the timer is declared in a long-running method,  
            ' KeepAlive must be used to prevent the JIT compiler 
            ' from allowing aggressive garbage collection to occur 
            ' before the method ends. (See end of method.)
            'Dim aTimer As System.Timers.Timer        ' Create a timer with a ten second interval.
            aTimer = New System.Timers.Timer(10000)        ' 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()        ' If the timer is declared in a long-running method, use
            ' KeepAlive to prevent garbage collection from occurring
            ' before the method ends.
            '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("The Elapsed event was raised at {0}", e.SignalTime)
        End Sub
    End Class' This code example produces output similar to the following:
    '
    'Press the Enter key to exit the program.
    'The Elapsed event was raised at 5/20/2007 8:42:27 PM
    'The Elapsed event was raised at 5/20/2007 8:42:29 PM
    'The Elapsed event was raised at 5/20/2007 8:42:31 PM
    '...
     
    C#  复制代码 
    using System;
    using System.Timers;public class Timer1
    {
        private static System.Timers.Timer aTimer;    public static void Main()
        {
            // Normally, the timer is declared at the class level,
            // so that it stays in scope as long as it is needed.
            // If the timer is declared in a long-running method,  
            // KeepAlive must be used to prevent the JIT compiler 
            // from allowing aggressive garbage collection to occur 
            // before the method ends. (See end of method.)
            //System.Timers.Timer aTimer;        // Create a timer with a ten second interval.
            aTimer = new System.Timers.Timer(10000);        // 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();        // If the timer is declared in a long-running method, use
            // KeepAlive to prevent garbage collection from occurring
            // before the method ends.
            //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("The Elapsed event was raised at {0}", e.SignalTime);
        }
    }/* This code example produces output similar to the following:Press the Enter key to exit the program.
    The Elapsed event was raised at 5/20/2007 8:42:27 PM
    The Elapsed event was raised at 5/20/2007 8:42:29 PM
    The Elapsed event was raised at 5/20/2007 8:42:31 PM
    ...
     */ 下面的代码示例实现简单的间隔计时器,该计时器每五秒钟发一次警报。当发生警报时,MessageBox 显示该警报已启动次数的计数,并询问用户计时器是否应继续运行。Visual Basic  复制代码 
    Public Class Class1
        Private Shared 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, _
                                               myEventArgs As EventArgs)
            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.
            AddHandler myTimer.Tick, AddressOf 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()
            End While    End Sub    End Class 
    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;
        }
     } 
      

  2.   

    http://www.cnblogs.com/hjf1223/archive/2008/03/06/framework_timer.html
      

  3.   

    System.Windows.Forms.Timer只有Tick事件
    System.Timers.Timer只有Elapsed事件它们是两个不同的Timer,但起到的效果是一样的
      

  4.   

    Timer 控件没有Elapsed事件吧,System.Timers类才有。Visual Studio 和 .NET Framework 中包含三个计时器控件:可添加到“工具箱”中的基于服务器的计时器始终位于“工具箱”中的基于 Windows 的计时器可通过编程方式使用的线程计时器基于 Windows 的计时器针对在 Windows 窗体应用程序中使用而进行了优化。基于服务器的计时器是传统的计时器为了在服务器环境上运行而优化后的更新版本。线程计时器是一种简单的、轻量级计时器,它使用回调方法而不是使用事件,并由线程池线程提供支持。在 Win32 体系结构中有两种类型的线程:UI 线程和辅助线程。UI 线程绝大多数时间处于空闲状态,等待消息循环中的消息到来。一旦接收到消息,它们就进行处理并等待下一个消息到来。另外,辅助线程用来执行后台处理而且不使用消息循环。Windows 计时器和基于服务器的计时器在运行时都使用 Interval 属性。线程计时器的时间间隔在 Timer 构造函数中设置。计时器的设计目的各不相同,它们的线程处理明确地指出了这一点: Windows 计时器是为单线程环境设计的,其中,UI 线程用于执行处理。Windows 计时器的精度限定为 55 毫秒。这些传统计时器要求用户代码有一个可用的 UI 消息泵,而且总是在同一个线程中操作,或者将调用封送到另一个线程。对于 COM 组件来说,这样会降低性能。 基于服务器的计时器是为在多线程环境下与辅助线程一起使用而设计的。由于它们使用不同的体系结构,因此基于服务器的计时器可能比 Windows 计时器精确得多。服务器计时器可以在线程之间移动来处理引发的事件。 对消息不在线程上发送的方案中,线程计时器是非常有用的。例如,基于 Windows 的计时器依赖于操作系统计时器的支持,如果不在线程上发送消息,与计时器相关的事件将不会发生。在这种情况下,线程计时器就非常有用。Windows 计时器位于 System.Windows.Forms 命名空间中,服务器计时器 System.Timers 命名空间中,线程计时器位于 System.Threading 命名空间中。