日期:2014-05-18  浏览次数:21007 次

[请教][难度问题]按钮事件中有循环的问题,用另一个按钮控制停止,暂停,继续
窗体上两按钮button1,button2和一个lable1;
按钮1的单击事件如下
private void button1_Click(object sender, EventArgs e)
  {
  label1.Refresh();
  label1.Text = "";
  for (int i = 0; i < 10000&&run==false; i++)
  {
  label1.Text = i.ToString();
  label1.Refresh();
  System.Threading.Thread.Sleep(100);
  }
  }
也就是按钮1不停循环改变lable1的文字;

问题:怎么实现用button2来控制button1事件的执行,如暂停,继续,停止等等!

------解决方案--------------------
这个,说来也容易。原理就不废话了。
C# code

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;
}

------解决方案--------------------
没有编译器调试,就随手写的,貌似上面写错了一点。用下面的
C# code


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)
{