Communication between threads?

G

Guest

Hi,

I have a background thread that opens a form. But when that thread ends, the
form I opened disappears. This makes me want the background thread to tell my
main thread to open the form instead.

The form should be opened by my main thread when my communication thread
receives a certain message. I’ve been trying to do this using delegates
(whose existence just recently came to my knowledge), but I’m failing and
have started wondering if it couldn’t be done using events instead.

This is my scenario:

StartForm (my main form)
CommForm (handles my serial communication)
FormA (I want StartForm to open this form)

When data is available on my serialport, the sub below is run as a separate
thread in my CommForm. When all data is read, the thread ends.

Private Shared Sub ReceiveEvent(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles SerialPort.DataAvailable
'here I want to tell my main thread (StartForm) to open FormA
End Sub

How should I solve this? Is an event more appropriate to use instead of a
delegate?
Iv'e been searching a bit on delegates and events, but I can't figure out
how to get them working between threads. All examples I've found are within
the same thread.

Many thanks in advance.
/Daniel
 
C

Cor Ligthert

Dast,

When I see this I get the idea that your option to run the threads, one
after each other, or is there another approach?

Cor
 
G

Guest

Hi Core,

I'm not sure I understand what you are asking for...
Could you perhaps explain?

/Daniel
 
G

Guest

If you mean that the RecieveEvent should be run in the same thread as my
StartForm and CommForm, thats something I can't control, since I'm using a
third-party program to handle the communication.

And I don't want the RecieveEvent to start FormA and wait until it closes.
I'ts a background thread, and shouldn't be involved in the UI.

Or was there something else you had in mind?

/Daniel
 

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

Similar Threads


Top