Communication between threads

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Could someone tell me how a thread can send data / post message to another
thread in vb.net please?

I have a main thread and a socket thread. I need a way to let the main
thread know when my socket thread receive data.

Thanks in advance!
 
do you need to ghave each on different threads? the Socket class has the
ability to get data async so it wont freeze ur UI, then the socket and main
thread are on the same thread, which just makes things easier
 
Do you mean I need to have a timer to check the socket receive buffer size in
UI thread?
 
iwdu15 said:
do you need to ghave each on different threads? the Socket class has the
ability to get data async so it wont freeze ur UI, then the socket and main
thread are on the same thread, which just makes things easier

That isn't really true. The async socket calls are still executed on a
separate thread from the main thread - you just don't explicitly create
that thread, it is taken from the thread pool.
 
I still need a way for threads to communicate with each other.

Other than the Main and Socket threads, I will have some more threads such
as Printer thread and Trace log thread.

I just realize I can open a file for writing from my Main thread and let the
socket thread use the FileNumber to do any writing. However, this problem
solved if I can have the socket thread send/post a message to my Main thread.

Someone, please help!!
 
Messages can be sent between threads using several mechanisms. You can
use a WaitHandle (either ManualResetEvent or AutoResetEvent) to cause a
thread to block until another thread signals an event. You can use
Monitor.Pulse and Monitor.Wait in much the same way. If one of your
threads is running a Windows message loop then you can use
Control.Invoke or Control.BeginInvoke. And of course data can be
shared between threads by making sure the variables are accessible from
the code that each thread executes. The variables themselves can even
be used a signalling mechanism. It really depends on exactly how you
want the threads to work together.

Brian
 
Back
Top