我有一个开始while循环的按钮,我该如何停止其他按钮的while循环. [英] i am having a button which starts a while loop how can i stop the while loop with other button.

查看:107
本文介绍了我有一个开始while循环的按钮,我该如何停止其他按钮的while循环.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

button1()
{
fun()
}

button2()
{

}

fun()
{
while()
print(something);
}

推荐答案



检查这些链接.

如何停止循环? [ ^ ]
如何从基于单击事件的无限循环中摆脱出来 [ C#从单独的类/方法 [ ^ ]
摆脱我的无限循环 [ C#在按按钮时退出循环 [ ^ ]

希望对您有所帮助.
Hi,

check these links.

How to stop a Loop?[^]
how to break from from an infinite loop based on a click event[^]
C# breaking out of a loop from seperate class/method[^]
breaking out of my infinite loop[^]
C# Break out of loop on button press[^]

Hope it helps.


以下是在线程中具有循环的示例:
Here is a sample having the loop in a thread:
private void button1_Click(object sender, EventArgs e)
{
   System.Threading.Thread thr = new Thread(ThreadProc);
   thr.Start();
}

private void button2_Click(object sender, EventArgs e)
{
   bCancel = true;
}

bool bCancel = false;
private void ThreadProc(object o)
{
   while (!bCancel)
   {
      //Do some work
      Thread.Sleep(1);
   }
   MessageBox.Show("Loop finished");
}




无限执行时不是可取的.我个人建议您使用BackgroundWorker进行长期运行. BackgroundWorker支持的范围比您想要的还要多.

这是执行此操作的一些好链接.

MSDN:BackgroundWorker [ http://www.dotnetperls.com/backgroundworker [ BackgroundWorker线程和支持取消 [
Hi,

Executing infinite while is not preferable. I personally suggest you to use BackgroundWorker to do your long run process. BackgroundWorker support more then what you want.

Here are some good link to do that.

MSDN : BackgroundWorker[^]

http://www.dotnetperls.com/backgroundworker[^]

BackgroundWorker Threads and Supporting Cancel[^]

Hope this works for you,
Thanks
-Amit Gajjar


这篇关于我有一个开始while循环的按钮,我该如何停止其他按钮的while循环.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆