本文实例为大家分享了C#线程倒计时器源码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
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; //计时时间 private TimeSpan endTime; //到点时间 private Label lb; private bool whereExit = true ; /// <summary> /// 设定计时器计时的时间 /// </summary> /// <param name="StartTime">计时器时间,如:01:00:00 既1小时</param> public TimeHeleper(TimeSpan StartTime, Label lb) { time = StartTime; this .lb = lb; } public void ShowLabel() { lb.Text = time.ToString(); } /// <summary> /// 获取时间 /// </summary> /// <returns></returns> public TimeSpan GetTime() { return time; } /// <summary> /// 开启计时器 /// </summary> public void Open() { //计算到点时间 TimeSpan tsNow = TimeSpan.Parse(DateTime.Now.ToString( "HH:mm:ss" )); TimeSpan tsAdd = time; endTime = tsNow + tsAdd; //线程开始 whereExit = false ; thread = new Thread(TimeThreadStart); thread.IsBackground = true ; thread.Start(); } /// <summary> /// 关闭计时器 /// </summary> 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控件 } } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/pang572936554/p/8249882.html