我已经阅读了有关此的其他帖子,但我似乎仍然无法使其正常工作。
每当我的 BackgroundWorker 开始工作时,我的函数API.CheckForUpdate都会导致 GUI 挂起。我不能点击任何东西。它只冻结了半秒钟,但足以引起注意。
我怎样才能解决这个问题?我应该更深入地研究 API.CheckForUpdate并在特定语句上运行单个线程,还是我可以只拥有一个包罗万象的线程来处理这个问题?API.CheckForUpdate 不引用 Form1 中的任何内容。
另外,我认为 Form1_Load 不是放置 RunWorkerAsync 调用的最佳位置。哪里有更好的地方?
'Declarations
Dim ApplicationUpdate As BackgroundWorker = New BackgroundWorker
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ApplicationUpdate.WorkerSupportsCancellation = True
ApplicationUpdate.WorkerReportsProgress = True
AddHandler ApplicationUpdate.DoWork, AddressOf ApplicationUpdate_DoWork
AddHandler ApplicationUpdate.ProgressChanged, AddressOf ApplicationUpdate_ProgressChanged
AddHandler ApplicationUpdate.RunWorkerCompleted, AddressOf ApplicationUpdate_RunWorkerCompleted
ApplicationUpdate.RunWorkerAsync()
End Sub
Private Sub ApplicationUpdate_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
'Check for an update (get the latest version)
Dim LatestVersion = API.CheckForUpdate
End Sub
Private Sub ApplicationUpdate_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
'Nothing here
End Sub
Private Sub ApplicationUpdate_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
'Work completed
MsgBox("Done")
End Sub