Question : Problem accesing form elements from other class (probably other thread)

Hi there, i have built a very simple program in
I have a serial port object which has a listener for dataReceived event.
The incoming data are passed to another function which processes it.

Now i want to show the outcome of the process on a textbox on the form.
It seems impossible and i think that the reason is that the listener runs on another thread of the one for the form, and since the function that processes the data is called from the listener, it also belongs to that other thread. Hence it has no knowledge of the forms existence.

Any ideas on how i can overcome this problem?


Answer : Problem accesing form elements from other class (probably other thread)

Declare a custom event in your Serial Port class that will pass the information OUT to the form.

When the instance of your class is created in the Form you use AddHandler() to wire up a method in the form to handle that event.

Thread marshaling can generally be done in one of three ways:
(1) From within the Form:  Use Me.InvokeRequired() along the Me.Invoke() and a Delegate.
(2) From within the Serial Port Class: Encapsulate a BackgroundWorker() control and let it marshal for via the ProgressChanged() event.
(3) From within the Serial Port Class: Use a SynchronizationContext() to switch threads before raising the event.

Some examples of the various techniques here:
Random Solutions  
programming4us programming4us