//线程属性
protected Thread workerThread;
protected bool isWorkerThreadStopped = true;
public void Initialize()
{
//初始化代码
}
public void Start()
{
isWorkerThreadStopped = false;
workerThread = new Thread(WorkerTask);
workerThread.IsBackground = true;
workerThread.Start();
}
public void Stop()
{
isWorkerThreadStopped = true;
}
protected virtual void WorkerTask()
{
while (!isWorkerThreadStopped)
{
//需要循环执行的代码
Thread.Sleep(1000);
}
}
设为后台线程,主线程退出则全部线程退出;
而前台线程,主线程退出,前台线程在Task Manager里还有运行。