Example Analysis of C # Thread Countdown timer
This article is to share with you the content of an example analysis of the C# thread countdown timer. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The details are as follows
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Windows.Forms;namespace ListZZBG {class TimeHeleper {Thread thread;private TimeSpan time; / / timing time private TimeSpan endTime; / / time to point private Label lb;private bool whereExit = true;/// set timer timing time / timer time, for example, 01:00:00 or 1 hour public TimeHeleper (TimeSpan StartTime, Label lb) {time = StartTime This.lb = lb;} public void ShowLabel () {lb.Text = time.ToString ();} / get time / public TimeSpan GetTime () {return time;} / start timer / public void Open () {/ / calculate to point time TimeSpan tsNow = TimeSpan.Parse ("HH:mm:ss"); TimeSpan tsAdd = time;endTime = tsNow + tsAdd;// thread start whereExit = false Thread = new Thread (TimeThreadStart); thread.IsBackground = true;thread.Start ();} / turn off timer / public void Close () {whereExit = true;thread.Join (1000);} private void TimeThreadStart () {while (! whereExit) {RunTime (); Thread.Sleep (1000);}} private delegate void RunTimeDelegate (); private void RunTime () {if (lb.InvokeRequired) {RunTimeDelegate d = RunTime;lb.Invoke (d) } else {time = endTime-TimeSpan.Parse (DateTime.Now.ToString ("HH:mm:ss")); string [] sp = time.ToString (). Split (':'); lb.Text = sp [2] .ToString (); / / liable1 controls} Thank you for reading! This is the end of this article on "sample analysis of C# thread countdown timer". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it out for more people to see!