通过单击按钮启动和停止递归功能 [英] Start and stop a recursive function by button click
问题描述
我必须一次又一次地执行一个功能。表示第一次执行完成后,第二次执行将开始。点击开始按钮我就可以开始了。但是如何停止执行点击停止按钮。
帮助我得到的是申请条件。但是这里没有条件可以应用。只需按一下按钮就可以停止它。
请举例说明如何操作。
I have to execute a function again and again. Means when first time execution finished,then second time execution will be started. Clicking on start button I can start. But how to stop execution clicking on stop button.
Help I'm getting is to apply condition. But here no condition can be applied. Just a button click will stop it.
Please give a small example how to do it.
推荐答案
您可以尝试做两件事:
1.设置一个布尔全局变量,并在调用和函数的另一个之间,如果该变量等于为true,停止执行。该按钮将使该变量等于true。
Hi,
You can try to do two things:
1. Set a boolean global variable and, between a call and the other of your function, if that variable equals to true, stop the execution. The button will make that variable equal to true.
public bool ShouldStop = false;
public void MyFunction(){
if (!ShouldStop)
//Execute
else
return;
}
2.正如Salman622在评论中提出的那样,使用BackgroundWorker或新线程来实现该功能并调用单击按钮时中止。
希望它有所帮助。
LG
2. As proposed by Salman622 in the comments, use a BackgroundWorker or a new thread for that function and call Abort when the button is clicked.
Hope it helps.
LG
这篇关于通过单击按钮启动和停止递归功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!