C#中线程的应用
使用计时器进行计时
在一个窗体中声明一个线程对象
private Thread trdTrans = null;
//计时器事件
private void tmrDataTrans_Tick(object sender, System.EventArgs e)
{
if( (trdTrans == null) || (trdTrans.ThreadState == ThreadState.Stopped) )
{
trdTrans = new Thread(new ThreadStart(Startup));
trdTrans.Start();
}
}
完全使用线程类
private Thread trdTrans = null;
private bool Run=false;
public void Start()
{
trdTrans = new Thread(new ThreadStart(Startup));
Run=true;
trdTrans.Start();
}
private void Startup()
{
while(Run)
{
thread.Sleep(1000);
}
}
public void Stop()
{
Run=false;
thread.Sleep(100);
trdtrans.Abrot();
}