有没有办法让一个'听众'来听取代码中间的按钮点击?在某些情况下,我不必等待代码完成才能退出函数,但我似乎无法找到一种方法来查看按钮是否被点击,而不是在整个函数调用中抛出大量的if检查这对我来说似乎不是很有效率。
答案 0 :(得分:0)
此代码在线程上运行,与主应用程序不同
private function LongRunningFunction()
Din canceled as boolean = false ' to make sure it is canceled while in the loop
' this is very long loop
For i as integer = 0 to 100000
If _cancelExecution Then
canceled = true
Exit For
End If
' your code runs here
.........
Next
If canceled Then
' Wrap up this thread, clean up stuff, etc
End If
.......................
End Function
在主线程上,单击按钮时_cancelExecution
设置为true
BackGroundWorker
已经内置了此机制