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
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