J
Joseph Byrns
I have Form1 (a normal form) which creates an instance of TestClass (a basic
class that is just a class, i.e. doesn't inherit from anything). In
TestClass I create a thread and from this thread I raise an event. I have a
sub in TestClass that handles this event and obviously this sub cannot
safely change any UI elements of Form1, so I would like to create another
event in the class that can be handled by the calling form and would be
thread safe.
I cannot use control.invoke in the TestClass as it is not a control and does
not inherit from control.
So what I really want to do is have TestClass handle all the events thrown
by the spawned thread and the Form1 handle the threadsafe events, hope that
makes sense, here is some code:
Class TestClass
Public Sub New()
Dim t as new Thread(AddressOf ContinuouslyCollectData)
t.start
End Sub
''This is a thread, looping indefinately
Public Sub ContinuouslyCollectData()
do
''Raise non-UI threadsage event periodically as:
RaiseEvent Test(sender and some test args here)
loop
End Sub
''the data arrived event args
Public Class DataArrivedEventArgs
Inherits EventArgs
Public ReadOnly Data As String
Public Sub New(ByVal Data As String)
Me.Data = Data
End Sub
Public Delegate Sub DataArrivedDelegate(ByVal sender As Object, ByVal
args As DataArrivedEventArgs)
''the local event (not thread safe)
Private Event m_DataArrived As DataArrivedDelegate
''handles the local event raised in the new thread and hopefully passes
it on to an event in the form (running in the UI thread).
Private Sub DataArrived_Handler(ByVal sender As Object, ByVal e As
DataArrivedEventArgs) Handles Me.m_DataArrived
''what do I invoke here so that it can be handled by the form that
created an instance of this class???
End Sub
End Class
In the a form I would like to do:
Private WithEvents TC as TestClass
Private Sub TC_DataArrived(ByVal Sender as object, e as
TestClass.DataArrivedEventArgs) Handles TC.DataArrived
''do stuff to the UI controls here........
End Sub
class that is just a class, i.e. doesn't inherit from anything). In
TestClass I create a thread and from this thread I raise an event. I have a
sub in TestClass that handles this event and obviously this sub cannot
safely change any UI elements of Form1, so I would like to create another
event in the class that can be handled by the calling form and would be
thread safe.
I cannot use control.invoke in the TestClass as it is not a control and does
not inherit from control.
So what I really want to do is have TestClass handle all the events thrown
by the spawned thread and the Form1 handle the threadsafe events, hope that
makes sense, here is some code:
Class TestClass
Public Sub New()
Dim t as new Thread(AddressOf ContinuouslyCollectData)
t.start
End Sub
''This is a thread, looping indefinately
Public Sub ContinuouslyCollectData()
do
''Raise non-UI threadsage event periodically as:
RaiseEvent Test(sender and some test args here)
loop
End Sub
''the data arrived event args
Public Class DataArrivedEventArgs
Inherits EventArgs
Public ReadOnly Data As String
Public Sub New(ByVal Data As String)
Me.Data = Data
End Sub
Public Delegate Sub DataArrivedDelegate(ByVal sender As Object, ByVal
args As DataArrivedEventArgs)
''the local event (not thread safe)
Private Event m_DataArrived As DataArrivedDelegate
''handles the local event raised in the new thread and hopefully passes
it on to an event in the form (running in the UI thread).
Private Sub DataArrived_Handler(ByVal sender As Object, ByVal e As
DataArrivedEventArgs) Handles Me.m_DataArrived
''what do I invoke here so that it can be handled by the form that
created an instance of this class???
End Sub
End Class
In the a form I would like to do:
Private WithEvents TC as TestClass
Private Sub TC_DataArrived(ByVal Sender as object, e as
TestClass.DataArrivedEventArgs) Handles TC.DataArrived
''do stuff to the UI controls here........
End Sub