任意循环,直到按下另一个按钮C#
有没有办法做任何循环,并打破它,直到另一个按钮 WinForms被按下了吗任意循环,直到按下另一个按钮C#,c#,cycle,C#,Cycle,有没有办法做任何循环,并打破它,直到另一个按钮 WinForms被按下了吗 do { //Action } while ("Button Stop is pressed"); 在不阻塞gui的情况下执行此操作的最佳方法是使用线程,例如: Thread t = new Thread (delegate() { while(true) { What you want to happen inside the loop in h
do
{
//Action
} while ("Button Stop is pressed");
在不阻塞gui的情况下执行此操作的最佳方法是使用线程,例如:
Thread t = new Thread (delegate() {
while(true) {
What you want to happen inside the loop in here;
Thread.Sleep(500); // this is useful for keeping the headroom of the CPU
}
});
以这种方式启动线程:
t.Start();
t.Abort();
按下停止按钮时,按以下方式中止线程:
t.Start();
t.Abort();
这将使gui保持活动状态。有两种可能的情况: 1-如果您在主线程中运行该循环,那么答案是否,如果您阻止主线程,则不会触发事件,也无法捕获按钮单击
2-您正在工作线程中运行该循环,在这种情况下,您可以在函数外创建一个布尔值,将其设置为true,检查循环中的该布尔值,并在单击按钮时将其设置为false。@user3363472:非常欢迎您,如果您想为
线程设置-1,您可以接受此(或其他)答案。中止,这是终止线程的可怕方式:正确的方式:。