背景工作者。停止线程执行

发布于 2025-02-13 10:17:11 字数 375 浏览 0 评论 0原文

下午好! 您能告诉我如何停止执行线程吗?以下代码无效:

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮年 2025-02-20 10:17:11

该代码完全按照应有的操作来完成,即请求取消。不过,它不能只是停止工作,因为它不知道正在做什么工作。您是编写代码进行工作的人,因此您是必须编写代码以检查是否已要求取消并实际执行取消的人。我们不知道您正在做什么工作,因此我们不知道在哪里可以方便地检查是否已要求取消,如果有的话,在这种情况下可能需要清理什么。这是取消可能是什么样的基本示例:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Raise the DoWork event in a worker thread.
    Me.BackgroundWorker1.RunWorkerAsync()
End Sub
 
'This method is executed in a worker thread.
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _
                                     ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
 
    For i As Integer = 1 To 100
        If worker.CancellationPending Then
            'The user has cancelled the background operation.
            e.Cancel = True
            Exit For
        End If
 
        'Raise the ProgressChanged event in the UI thread.
        worker.ReportProgress(i, i & " iterations complete")
 
        'Perform some time-consuming operation here.
        Threading.Thread.Sleep(250)
    Next i
End Sub
 
'This method is executed in the UI thread.
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _
                                              ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Me.ProgressBar1.Value = e.ProgressPercentage
    Me.Label1.Text = TryCast(e.UserState, String)
End Sub
 
'This method is executed in the UI thread.
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
                                                 ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    If e.Cancelled Then
        'The background operation was cancelled.
        Me.Label1.Text = "Operation cancelled"
    Else
        'The background operation completed normally.
        Me.Label1.Text = "Operation complete"
    End If
End Sub
 
Private Sub Button1_Click(ByVal sender As Object, _
                          ByVal e As EventArgs) Handles Button1.Click
    'Only cancel the background opertion if there is a background operation in progress.
    If Me.BackgroundWorker1.IsBusy Then
        Me.BackgroundWorker1.CancelAsync()
    End If
End Sub

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:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Raise the DoWork event in a worker thread.
    Me.BackgroundWorker1.RunWorkerAsync()
End Sub
 
'This method is executed in a worker thread.
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _
                                     ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
 
    For i As Integer = 1 To 100
        If worker.CancellationPending Then
            'The user has cancelled the background operation.
            e.Cancel = True
            Exit For
        End If
 
        'Raise the ProgressChanged event in the UI thread.
        worker.ReportProgress(i, i & " iterations complete")
 
        'Perform some time-consuming operation here.
        Threading.Thread.Sleep(250)
    Next i
End Sub
 
'This method is executed in the UI thread.
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _
                                              ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Me.ProgressBar1.Value = e.ProgressPercentage
    Me.Label1.Text = TryCast(e.UserState, String)
End Sub
 
'This method is executed in the UI thread.
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
                                                 ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    If e.Cancelled Then
        'The background operation was cancelled.
        Me.Label1.Text = "Operation cancelled"
    Else
        'The background operation completed normally.
        Me.Label1.Text = "Operation complete"
    End If
End Sub
 
Private Sub Button1_Click(ByVal sender As Object, _
                          ByVal e As EventArgs) Handles Button1.Click
    'Only cancel the background opertion if there is a background operation in progress.
    If Me.BackgroundWorker1.IsBusy Then
        Me.BackgroundWorker1.CancelAsync()
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文