日期:2014-05-18 浏览次数:20980 次
public partial class MainForm : Form { public MainForm() { this.InitializeComponent(); } private Thread m_thread = null; private ThreadState m_state = ThreadState.Unstarted; private void HandleTask() { this.m_state = ThreadState.Running; int i = 0; while (i < 100000) { //已经暂停 if(this.m_state == ThreadState.Suspended) continue; //请求暂停 if (this.m_state == ThreadState.SuspendRequested) { this.m_state = ThreadState.Suspended; continue; } //已经停止 if (this.m_state == ThreadState.Stopped) break; //请求停止 if (this.m_state == ThreadState.StopRequested) { this.m_state = ThreadState.Stopped; break; } //处理主要任务,这里休息100ms是为了模拟实际任务的执行时间 Thread.Sleep(100); i = i + 1; //在界面上显示信息 this.Invoke(new ShowInfoHandler(this.ShowInfo), new object[] { i }); } this.m_thread = null; this.m_state = ThreadState.Stopped; } public void Start() { if (this.m_thread == null) { this.m_thread = new Thread(new ThreadStart(this.HandleTask)); this.m_thread.Start(); } if (this.m_state == ThreadState.Suspended) this.m_state = ThreadState.Running; } public void Pause() { this.m_state = ThreadState.SuspendRequested; while (this.m_state != ThreadState.Suspended) Application.DoEvents(); } public void Stop() { this.m_state = ThreadState.StopRequested; while (this.m_state != Th