Problem: HowTo update UI from workerThread?

P

Prozon

Hi!
I want to update an userinterface from a background thread.

I think that a good way to do this is to use to use methodinvoker in
UI-thread and then begininvoke in workerThread to pass parameters back
and update the UI. But i have one question. The Method i start with
begininvoke is a public sub on another class (not the UI-class). Do I
have to pass the form (like a reference) to the other class which the
workerthread uses to be able to use it´s begininvoke method? Is this a
good way to do this. Here is my code:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
' Some code
Me.Label1 = New System.Windows.Forms.Label
' Some more code
#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim workerThread As New MyWorkerClass
Dim mi As MethodInvoker = New MethodInvoker(AddressOf
workerThread.StartWork)
mi.BeginInvoke(Nothing, Nothing)
End Sub

Public Sub UpdateLabel(ByVal o As Object, ByVal e As
System.EventArgs)
Dim myEvent As MyLabelEvent = CType(e, MyLabelEvent)
Label1.Text = e.LabelText()
End Sub
End Class

Public Class MyWorkerClass
Public Sub StartWork()
Dim e As System.EventArgs
While True
e = New MyLabelEvent("Text on label")
Dim lst As Object() = {Me, e}
???.BeginInvoke(New System.EventHandler(AddressOf
???.UpdateLabel), lst)

DoSomeWork()

End While
End Sub

Public Sub DoSomeWork()

End Sub
End Class

Public Class MyLabelEvent
Inherits System.EventArgs

Private _labelText As String

Public Sub New(ByVal label As String)
MyBase.New()
_labelText = label
End Sub

Public Property LabelText() As String
Get
Return _labelText
End Get
Set(ByVal Value As String)
_labelText = Value
End Set
End Property
End Class



Best Regards
/ Steve
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Prozon,

First there is no such a thing as an UI class. All method in all classes can
be executed in all threads.
In order to update the UI thread the code that does updating has to be
executed form the UI thread. To do that you have to use Control.Invoke. To
call the invoke method you can use any control created by the UI thread and
you can execute any code there regardless of the class that declares the
method to execute.
 
J

Jon Skeet [C# MVP]

Prozon said:
I want to update an userinterface from a background thread.

I think that a good way to do this is to use to use methodinvoker in
UI-thread and then begininvoke in workerThread to pass parameters back
and update the UI.

No - at least, you shouldn't call BeginInvoke on the delegate; you
should call it on the form or the control, passing in the delegate you
wish to be executed.

See
http://www.pobox.com/~skeet/csharp/multithreading.html#windows.forms
for more information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top