C# wpf的三种timer+循环显示数组内容
转自:http://blog.csdn.net/supegg/article/details/6460649
三种timer: 定义: private DispatcherTimer dispatcherTimer = new DispatcherTimer(); //从ui线程启动,当ui线程Thread.Sleep(5000)时,timer也会中断。 private System.Timers.Timer timersTimer = new System.Timers.Timer();//从threadpool启动 private System.Threading.Timer threadingTimer;//从threadpool启动 初始化: //dispatcherTimer 的初始化 dispatcherTimer.Interval = new TimeSpan(0, 0, 1); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); //timersTimer 的初始化 timersTimer.Interval = 1000; timersTimer.Elapsed += new ElapsedEventHandler(timersTimer_Elapsed); //threadingTimer 的初始化 threadingTimer = new System.Threading.Timer(new System.Threading.TimerCallback(ThreadMethod), null, -1, 1000); // object state 是传给被调用方法的信息,即参数 启动: dispatcherTimer.Start(); timersTimer.Enabled = true; threadingTimer.Change(0, 1000); 停止: dispatcherTimer.Stop(); timersTimer.Enabled = false; threadingTimer.Change(-1, 1000);
private string[] arrayC = new string[3]; private int count=0; private void Form1_Load(object sender, System.EventArgs e) { arrayC[0] = "Shirdrn"; arrayC[1] = "Hamtty"; arrayC[2] = "Saxery"; this.timer1.Enabled=true; this.timer1.Interval=500; } private void timer1_Tick(object sender, System.EventArgs e) { int j=count%arrayC.Length;//循环得到该第几个数出现 this.label1.Text=arrayC[j].ToString(); count++; }
从threadpool启动的timer,调用UI元素需要delegate。
http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(DELEGATE_CSHARPKEYWORD);k(SOLUTIONITEMSPROJECT);k(DevLang-CSHARP)&rd=true
实验发现,当中断窗体线程时,会影响timer的定时精度。
工程链接:http://download.csdn.net/source/3333227
其他参考:http://blog.sina.com.cn/s/blog_5aeeb8200100bhc4.html

浙公网安备 33010602011771号