C#如何设置一个定时器,功能类似C++的SetTimer()函数,每隔一段时间系统自动调用回调函数。方法越简单越好。。

解决方案 »

  1.   

    TimerCallback testTime = new TimerCallback(test);
    System.Threading.Timer timer = new System.Threading.Timer(testTime, "", 0, 1000);void test(object status)
    {
    }
      

  2.   

    控制台运行下面代码:using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;namespace TimerApp
    {
      class Program
      {
        static void Main(string[] args)
        {
          Console.WriteLine("***** Working with Timer type *****\n");      // Create the delegate for the Timer type.
          TimerCallback timeCB = new TimerCallback(PrintTime);      // Establish timer settings.
          Timer t = new Timer(
            timeCB,             // The TimerCallback delegate type.
            "Hello From Main",  // Any info to pass into the called method (null for no info).
            0,                  // Amount of time to wait before starting.
            1000);   //一秒钟调用一次 Interval of time between calls (in milliseconds).      Console.WriteLine("Hit key to terminate...");
          Console.ReadLine();
        }    static void PrintTime(object state)
        {
          Console.WriteLine("Time is: {0}, Param is: {1}",
            DateTime.Now.ToLongTimeString(), state.ToString());
        }
      }
    }