ThreadSafe Query

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
 
C

Chris Tacke, eMVP

Your class doesn't need to be a Control. Add a private Control as a member,
then use its Invoke to migrate to the UI thread within your class.

--
Chris Tacke
Co-founder
OpenNETCF.org
Are you using the SDF? Let's do a case study.
Email us at d c s @ o p e n n e t c f . c o m
http://www.opennetcf.org/donate
 
J

Joseph Byrns

That makes sense, although I have managed to make some progress by having
the class inherit from control, is there any advantage to creating a private
member to inheriting?

Thanks,

Joseph
 
C

Chris Tacke, eMVP

C# doesn't support multiple inheritance, so you use up any inheriting stuff,
plus if you inherit from control, you get all the other stuff from a Control
that you may not want to expost to your class consumers, else they might try
to use them.

--
Chris Tacke
Co-founder
OpenNETCF.org
Are you using the SDF? Let's do a case study.
Email us at d c s @ o p e n n e t c f . c o m
http://www.opennetcf.org/donate
 

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