GLog.WLog("_thdTimer before"); _thdTimer = new System.Threading.Timer(new TimerCallback(TimerProc)); //2秒后开始执行计时器;每3秒执行一次 _thdTimer.Change(2000,3000); GLog.WLog("_thdTimer after"); private void TimerProc(object state) { try { GLog.WLog("threading timer tick"); } catch (Exception ex) { } }
2017-04-24 00:20:34.821 _thdTimer before
2017-04-24 00:20:34.821 _thdTimer after
2017-04-24 00:20:36.827 threading timer tick
2017-04-24 00:20:39.839 threading timer tick
2017-04-24 00:20:42.849 threading timer tick
2017-04-24 00:20:45.861 threading timer tick
2017-04-24 00:20:48.871 threading timer tick
2017-04-24 00:20:51.880 threading timer tick
如果想延时2秒执行一个方法,且只执行一次。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleThreadTimer { class Program { static void Main(string[] args) { Console.WriteLine("HELLO"); System.Threading.Timer _thdTimer = new System.Threading.Timer(new TimerCallback(TimerProc), "0",2000, Timeout.Infinite); Console.ReadKey(); } private static void TimerProc(object state) { try { string s = state.ToString(); Console.WriteLine("threading timer tick:"+ s); } catch (Exception ex) { } } } }
-