gpt4 book ai didi

VB.net 停止后台 worker

转载 作者:行者123 更新时间:2023-12-03 18:25:25 26 4
gpt4 key购买 nike

我想创建一个按钮,可以停止我的后台工作人员并结束它正在处理的所有进程。

这是我的示例后台工作代码:

       Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Try
If BackgroundWorker1.IsBusy <> True Then
BackgroundWorker1.RunWorkerAsync()
End If
Catch ex As Exception
End Try

End Sub

Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

Dim counter As Integer = 1

Do

'updated code with stop function----------------
BackgroundWorker1.WorkerSupportsCancellation = True
If BackgroundWorker1.CancellationPending Then
e.Cancel = True
ProgressBar1.Value = 0
Exit Do
End If
'updated code with stop function----------------

ListBox1.Items.Add(counter)

ProgressBar1.Value = ((counter - 1) / limit) * 100
counter = counter + 1
Loop While(counter <= 999999999999999999)

End Sub

Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Try
Catch ex As Exception
End Try
End Sub

Private Sub BackgroundWorker1_Completed(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Try
Catch ex As Exception
End Try
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
End Sub

'updated code with stop function----------------
Private Sub StopButton_Click(sender As Object, e As EventArgs) Handles StopButton.Click
If BackgroundWorker1.IsBusy Then

If BackgroundWorker1.WorkerSupportsCancellation Then
BackgroundWorker1.CancelAsync()
End If
End If
End Sub
'updated code with stop function----------------

我想在停止后台工作时重置循环并将进度条返回到 0%。

这可能吗?

上面的代码已经更新,现在可以正常工作了。

我在 do 循环中添加了这段代码:
        BackgroundWorker1.WorkerSupportsCancellation = True
If BackgroundWorker1.CancellationPending Then
e.Cancel = True
ProgressBar1.Value = 0
Exit Do
End If

我创建了一个停止工作人员的按钮:
    Private Sub StopButton_Click(sender As Object, e As EventArgs) Handles StopButton.Click
If BackgroundWorker1.IsBusy Then

If BackgroundWorker1.WorkerSupportsCancellation Then
BackgroundWorker1.CancelAsync()
End If
End If
End Sub

最佳答案

Backgroundworker 类具有方法 CancelAsync()您需要调用它来取消 bgw 的执行。

您需要设置 Backgroundworker.WorkerSupportsCancellation属性为 true 并且在 while 循环内您需要检查 CancellationPending属性值是否为 true表示调用 CancelAsync()方法。

CancellationPending评估为真,你会( 你应该已经做的 )调用重载的 ReportProgress() 之一( Docu ) 方法将您的 ProgressBar 值设置为所需的值。

编辑:您应该设置 Cancel DoWorkEventArgs 的属性(property)为 true 以便您可以检查 Cancelled RunWorkerCompletedEventArgs 的属性(property)内RunworkerCompleted事件。

您也不应该访问位于 UI 线程中的任何控件。你最好使用 ProgressChanged ( Docu ) 事件。

见:BackgroundWorker Docu

关于VB.net 停止后台 worker ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18437290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com