我目前正在使用这个代码,(这是有效的)但我对它的外观并不满意......有没有更专业的方法呢?
这是我现在使用的代码:
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 SubAs 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 SubAnd, to cancel the operation, I run WebClient.CancelAsync().
更多推荐
发布评论