本文实例为大家分享了c#线程倒计时器源码,供大家参考,具体内容如下
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控件 } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
网友评论