Brady
Brady

Reputation: 93

BackgroundWorker freezes GUI

I have read other posts about this but I still can't seem to get it to work right.

Whenever my BackgroundWorker begins to do work, my function API.CheckForUpdate causes the GUI to hang. I can't click on anything. It only freezes for half a second, but is enough to notice.

How can I fix this? Should I dive deeper into API.CheckForUpdate and run individual threads on particular statements, or can I just have an all-inclusive thread that handles this? API.CheckForUpdate does not reference anything in Form1.

Also, I presume Form1_Load is not the best place to put the RunWorkerAsync call. Where is a better spot?

'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

Upvotes: 0

Views: 1225

Answers (2)

Don Thomas Boyle
Don Thomas Boyle

Reputation: 3045

Its not a background worker Fix but if you don't mind walking around and not finding the answer, you can code like so:

Keep in mind when you first Start a Thread and you are coding in a Model you MUST pass (me) into the initial thread because of VB having a concept of "Default Form Instances". For every Form in the application's namespace, there will be a default instance created in the My namespace under the Forms property.

and that is just adding an additional parameter like so

----------------------/ Starting Main Thread /-----------------------------------

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

------------------/ Running Thread /---------------

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

------------------/ How to Set UI Calls from a Thread / ---------------------

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

Sources From Pakks Answer Tested!

Upvotes: 1

SysDragon
SysDragon

Reputation: 9888

Try starting the process outside the Load event. Create a Timer and start it on the Load event, and then handle the event for the tick:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Timer1.Enabled = False
    ApplicationUpdate.RunWorkerAsync()
End Sub

Upvotes: 0

Related Questions