gpt4 book ai didi

vb.net - 后台工作程序 CancelAsync() 不起作用

转载 作者:行者123 更新时间:2023-12-04 23:55:40 25 4
gpt4 key购买 nike

我正在尝试使用 WorkerClass.bw.CancelAsync() 取消我的后台工作程序.但它根本不起作用。

//编辑!我在这里发布了完整的代码。愿这会有所帮助。
好的,我添加了一些 Msgboxes 以了解 Worker 是否仍然很忙并且有线的东西是,我得到一个 false worker 在做事的时候!?!?

Public Class Form1

Private Sub btn_start_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_start.Click
Dim WorkerClass As New BGWClass
WorkerClass.bw.WorkerSupportsCancellation = True
WorkerClass.bw.WorkerReportsProgress = True
If btn_start.Text = "Start" Then
btn_start.Image = My.Resources.Resources.gem_remove
btn_add_addy.Enabled = False
btn_start.Text = "Stop"
WorkerClass.Start()
WorkerClass.bw.RunWorkerAsync()
MsgBox(WorkerClass.bw.IsBusy & " " & WorkerClass.bw.WorkerSupportsCancellation)
Else
btn_start.Image = My.Resources.Resources.error_fuck
btn_add_addy.Enabled = True
btn_start.Enabled = False
MsgBox(WorkerClass.bw.IsBusy & " " & WorkerClass.bw.WorkerSupportsCancellation)
WorkerClass.bw.CancelAsync()
End If
End Sub
End Class

Public Class BGWClass
Public bw As BackgroundWorker = New BackgroundWorker

Sub Start()
AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
End Sub

Private Sub bw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
For x As Integer = 1 To 15
If bw.CancellationPending Then
e.Cancel = True
Exit Sub
End If
bw.ReportProgress(x)
Threading.Thread.Sleep(1000)
Next
End Sub

Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs)
Dim myObject As Object = e.UserState
Form1.Prgs_error.Text = "- Error: " + e.ProgressPercentage.ToString
End Sub

Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
'...
End Sub
End Class

最佳答案

我认为你的问题是你只是在评估 CancellationPending一次,在您的 bw_DoWork 开始时方法。因为实际上没有办法取消 BackgroundWorker在开始之前,CancellationPending永远是假的,永远不会中断工作。当您调用 CancelAsync 时,BackgroundWorker 不会使用一些魔法来接管程序计数器。 .

为了使其正常工作,您 DoWork 的核心逻辑方法必须以允许频繁轮询 CancellationPending 的方式实现。这样代码就会知道什么时候退出它正在做的事情。你需要从这里开始:

Private Sub bw_DoWork(ByVal sender As Object, 
ByVal e As System.ComponentModel.DoWorkEventArgs)

If bw.CancellationPending = True Then
e.Cancel = True
Else
'do stuff here
End If

End Sub

更像这样的事情:
Private Sub bw_DoWork(ByVal sender As Object, 
ByVal e As System.ComponentModel.DoWorkEventArgs)

Dim workIsCompleted As Boolean = False
While (Not bw.CancellationPending) AndAlso (Not workIsCompleted) Then

' Do stuff here, but incrementally so that the while loop can
' periodically check to see if CancelAsync has been called.
' Also, be sure to set workIsCompleted = True when the work is done.
' Otherwise, you will just spin forever until someone cancels it
' (Which may or may not be a bad thing, depending on your requirements)

End While

End Sub

关于vb.net - 后台工作程序 CancelAsync() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16893953/

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