背景工作者。停止线程执行
下午好! 您能告诉我如何停止执行线程吗?以下代码无效:
Private Sub Stop_Click(sender As Object, e As EventArgs) Handles Stop.Click
If BackgroundWorker1.IsBusy Then
If BackgroundWorker1.WorkerSupportsCancellation Then
BackgroundWorker1.CancelAsync()
End If
End If
End Sub
Good afternoon!
Can you tell me how to stop the execution of a thread? The following code doesn't work:
Private Sub Stop_Click(sender As Object, e As EventArgs) Handles Stop.Click
If BackgroundWorker1.IsBusy Then
If BackgroundWorker1.WorkerSupportsCancellation Then
BackgroundWorker1.CancelAsync()
End If
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码完全按照应有的操作来完成,即请求取消。不过,它不能只是停止工作,因为它不知道正在做什么工作。您是编写代码进行工作的人,因此您是必须编写代码以检查是否已要求取消并实际执行取消的人。我们不知道您正在做什么工作,因此我们不知道在哪里可以方便地检查是否已要求取消,如果有的话,在这种情况下可能需要清理什么。这是取消可能是什么样的基本示例:
That code does exactly what it is supposed to do, i.e. REQUEST a cancellation. It can't just stop working though, because it has no idea what work is being done. YOU are the one writing the code to do the work so YOU are the one who has to write the code to check whether a cancellation has been requested and to actually perform that cancellation. We have no idea what work you're doing so we have no idea where it is convenient to check whether a cancellation has been requested and what, if any, clean-up may be required in that case. Here's a basic example of what a cancellation might look like though: