1

I have searched the site and found similiar topics, but my problem is that the explanation / solution is a bit beyond my technical understanding at the moment.

I have only just noticed a "Background Worker" in Visual Studio, and I am using it to login to my database and keep the UI functioning (GIF image to show loading etc).

Problem I am having is that once the login is complete i am updating a label to say "login Complete" etc.

I am getting an error "Cross thread operation not valid"

If possible, could anybody tell me a way i can update a control via the Background Worker, or provide a source that I can use as a resource?

I have never used multi threading before, i understand the reason for the problem, but I dont see how to work around it.

Thanks for your time!

Tom

1 Answers1

1

You have to update your display in the BackgroundWorker event handlers for the ProgressChanged and RunWorkerCompleted events. This will eliminate the cross-thread problems you are experiencing.

Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, _
ByVal e As RunWorkerCompletedEventArgs) Handles backgroundWorker1.RunWorkerCompleted
    If e.Cancelled = True Then
        resultLabel.Text = "Canceled!"
    ElseIf e.Error IsNot Nothing Then
        resultLabel.Text = "Error: " & e.Error.Message
    Else
        resultLabel.Text = "Done!"
    End If
End Sub

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx#Y2497.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • Thanks, i tried the progress changed method, but that seems to only work for Progress controls i guess as it asks for a percentage. As yourself and Larstech suggested the runworker event seems to work for me. I am updating a variable with a value, and on RunWorkerCompleted i am assigning the value to the control. Thanks both for your help! –  Aug 15 '12 at 18:07