欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#线程倒计时器源码分享

程序员文章站 2023-12-20 09:23:28
本文实例为大家分享了c#线程倒计时器源码,供大家参考,具体内容如下 using system; using system.collections.generic...

本文实例为大家分享了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控件
}
}
}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: