亲宝软件园·资讯

展开

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

王子先生 人气:0
想了解C#线程倒计时器源码讲解的相关内容吗,王子先生在本文为您仔细讲解C#线程倒计时器的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#线程倒计时器,C#倒计时器,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控件
}
}
}

}

加载全部内容

相关教程
猜你喜欢
用户评论