日期:2014-05-18 浏览次数:21007 次
public enum RunState { running, pause, suppend } RunState state = RunState.suppend; bool Restart = false; RunProc(...)//执行的部分 { while(state != RunState.suppend) { //执行操作 Application.DoEvents(); if(state == RunState.suppend) break;//保证可以正常退出 while(state == RunState.pause)//保证可以暂停,不过这样效率不高:)但实现这个功能也只能如此了。 { Application.DoEvents(); } } if(Restart) { Restart = False; Button2.PerformClick(); } } Button2_Clicked(...)//开始 { if(state != RunState.suppend) { state = RunState.suppend; Restart = true; } state = RunState.running; RunProc(); } Button3_Clicked(...)//暂停 { state = RunState.pause; } Button4_Clicked(...)//停止 { state = RunState.suppend; }
------解决方案--------------------
没有编译器调试,就随手写的,貌似上面写错了一点。用下面的
public enum RunState { running, pause, suppend } RunState state = RunState.suppend; bool Restart = false; RunProc(...)//执行的部分 { while(state != RunState.suppend) { //....................// //执行你的操作 Application.DoEvents(); if(state == RunState.suppend) break;//保证可以正常退出 while(state == RunState.pause)//保证可以暂停,不过这样效率不高:)但实现这个功能也只能如此了。 { Application.DoEvents(); } } if(Restart) { Restart = False; Button2.PerformClick(); } } Button2_Clicked(...)//开始 { if(state != RunState.suppend) { state = RunState.suppend; Restart = true; return;//修改了这里,要退出栈 } state = RunState.running; RunProc(); } Button3_Clicked(...)//暂停 { state = RunState.pause; } Button4_Clicked(...)//停止 { state = RunState.suppend; }
------解决方案--------------------
改了下,Restart没看出有什么实际的用处所以删掉了。
这个最好还是用线程或者BackgroundWorker,用法很简单,参考MSDN
public enum RunState
{
running,
pause,
suppend
}
RunState state = RunState.suppend;
int i = 0;
private void RunProc()//执行的部分
{
while(i<10000)
{
label1.Text = i.ToString();
label1.Refresh();
System.Threading.Thread.Sleep(200);
Application.DoEvents();
if (state == RunState.suppend)
{
i = 0;
label1.Text = i.ToString();
label1.Refresh();
break;//保证可以正常退出
}
while (state == RunState.pause)//保证可以暂停,不过这样效率不高:)但实现这个功能也只能如此了。
{
Application.DoEvents();
}
i++;
}
}
private void button1_Click(object sender, EventArgs e)
{