0

我已经阅读了有关此的其他帖子,但我似乎仍然无法使其正常工作。

每当我的 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
4

2 回答 2

1

它不是后台工作人员修复,但如果您不介意四处走动却找不到答案,您可以像这样编写代码:

请记住,当您第一次启动线程并在模型中编码时,您必须将(我)传递给初始线程,因为 VB 具有“默认表单实例”的概念。对于应用程序命名空间中的每个 Form,将在 Forms 属性下的 My 命名空间中创建一个默认实例。

那只是像这样添加一个额外的参数

---------------------- / 启动主线程 / ----------- ------------

Private Sub FindCustomerLocation()
Dim Findcontractor_Thread As New Thread(AddressOf **FindContractor_ThreadExecute**)
Findcontractor_Thread.Priority = ThreadPriority.AboveNormal
Findcontractor_Thread.Start(me)
End Sub

------------------ / 运行线程 / ---------------

Private Sub **FindContractor_ThreadExecute**(beginform as *NameOfFormComingFrom*)
Dim threadControls(1) As Object
threadControls(0) = Me.XamDataGrid1
threadControls(1) = Me.WebBrowserMap

**FindContractor_WorkingThread**(threadControls,beginform) ' ANY UI Calls back to the Main UI Thread MUST be delegated and Invoked
End Sub

------------------ / 如何从线程设置 UI 调用 / ------------------

Delegate Sub **FindContractor_WorkingThread**(s As Integer,beginform as       *NameOfFormComingFrom*)
Sub **FindContractor_WorkingThreadInvoke**(ByVal s As Integer,beginform as     *NameOfFormComingFrom*)
If beginform.mouse.InvokeRequired Then
Dim d As New FindContractor_WorkingThread(AddressOf        FindContractor_WorkingThreadInvoke)
beginform.Invoke(d, New Object() {s,beginform})
Else
 beginform.Mouse.OverrideCursor = Cursors.Wait

'Do something...

beginform.Mouse.OverrideCursor = Nothing
End If
End Sub

来自 Pakks 答案测试的来源!

于 2013-07-25T18:08:43.127 回答
0

尝试在 Load 事件之外启动进程。创建一个 Timer 并在 Load 事件上启动它,然后处理 tick 事件:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Timer1.Enabled = False
    ApplicationUpdate.RunWorkerAsync()
End Sub
于 2013-07-16T05:33:53.490 回答