如何正确退出取消的BackgroundWorker异步?(How to properly exit from canceled BackgroundWorker async?)

编程入门 行业动态 更新时间:2024-10-27 05:23:39
如何正确退出取消的BackgroundWorker异步?(How to properly exit from canceled BackgroundWorker async?)

我目前正在使用这个代码,(这是有效的)但我对它的外观并不满意......有没有更专业的方法呢?

这是我现在使用的代码:

Private Sub BackgroundWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker.DoWork Try If BackgroundWorker.CancellationPending Then e.Cancel = True Exit Sub End If LatestVersion = Web.DownloadString(UpdateLink) If BackgroundWorker.CancellationPending Then e.Cancel = True Exit Sub End If If LatestVersion.Contains(InstalledVersion) Then e.Result = UP_TO_DATE Else e.Result = OUTDATED End If Web.Dispose() Catch ex As Exception e.Result = ex.Message End Try End Sub

如你所见,我重复两次相同的条件。 但想象一下,如果有更多的代码,我应该多次重复它...

我的问题是我想在任何时候退出Sub,只要BackgroundWorker.CancellationPending属性设置为True 。

我使用相同的条件两次,因为我想检查操作是否已经取消之前,以及下载我的字符串后(我不想等待字符串下载,而我已经取消了操作..这是浪费时间)。

我应该使用While语句吗? 如果有,怎么样?

I'm currently using this code, (which is working) but I'm not satisfied with how it looks... Is there a more professional way to do it ?

Here's the code I use now :

Private Sub BackgroundWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker.DoWork Try If BackgroundWorker.CancellationPending Then e.Cancel = True Exit Sub End If LatestVersion = Web.DownloadString(UpdateLink) If BackgroundWorker.CancellationPending Then e.Cancel = True Exit Sub End If If LatestVersion.Contains(InstalledVersion) Then e.Result = UP_TO_DATE Else e.Result = OUTDATED End If Web.Dispose() Catch ex As Exception e.Result = ex.Message End Try End Sub

As you can see, I'm repeating two times the same condition. But imagine if there was more code, I should have repeat it more times...

My problem is that I would like to exit the Sub at anytime, as long as the BackgroundWorker.CancellationPending property is set to True.

I'm using the same condition two times because I wanna check if the operation has been canceled before, and after downloading of my string (I don't wanna wait for the string to be downloaded whereas I've already canceled the operation... it's a waste of time).

Should I use a While statement ? If yes, how ?

最满意答案

不要使用BackgroundWorker ,这个问题就消失了。 代码可能应该是(VB和C#的混合):

//Instance field: CancellationTokenSource cts = ...; //Update method var downloadTask = HttpClient().DownloadString(UpdateLink, cts.Token); await Task.WhenAll(downloadTask, cts.Token); //Wait with timeout LatestVersion = await downloadTask; If LatestVersion.Contains(InstalledVersion) Then ShowResult(UP_TO_DATE); Else ShowResult(OUTDATED); End If

如果你想取消,请发信号。

此外,缺少错误处理。 这很容易测试和添加。

I've found another solution. By the way, I've renamed Web by WebClient...

Here's my code :

Private Sub Form1_Load() Handles Form1.Load AddHandler WebClient.DownloadStringCompleted, AddressOf WebClient_DownloadStringCompleted End Sub Private Sub BackgroundWorker_DoWork() Handles BackgroundWorker.DoWork WebClient.DownloadStringAsync(New Uri(UpdateLink)) End Sub Private Sub WebClient_DownloadStringCompleted(sender As Object, e As System.Net.DownloadStringCompletedEventArgs) Dim Output As String = Nothing If Not e.Cancelled Then LatestVersion = e.Result If LatestVersion.Contains(InstalledVersion) Then Output = UP_TO_DATE Else Output = OUTDATED End If End If End Sub

And, to cancel the operation, I run WebClient.CancelAsync().

更多推荐

本文发布于:2023-07-08 00:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1070193.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何正确   BackgroundWorker   properly   async   canceled

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!