Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Threading 1

Status
Not open for further replies.

koleraba

Programmer
Jun 14, 2008
8
SI
Hi

I have created a class that creates another thread. That background thread than creates a tcp client, connects to listener and waits for a message that will be send by the listener. When the message is received, it raises an event and waits for new messages. The problem is that the event is raised in background thread, instead in the thread that created that instance of my class. Does anybody has any idea how could I notify my main thread, so that thread could raise the event or if anybody has any other solution. I guess I need to implement something like Invoke method that every control has. but I don't know how I can do that. Any advice will be appreciated.

Thanks in advance

Uros
 
There are really 2 ways to handle the callback to a thread.

1. Use Invoke() on a delegate that you create in your main class. Google Delegates C#.

2. Use an Event - when the event is to be thrown you are basically invoking your event from the new thread. This is very similar to the first method but the delegates and communication happens in different places. Be aware that just throwing an event from your thread without properly invoking it is not a good solution.



 
Thanks for your reply. But I am afraid I don't really understand the second answer. Could you maybe show an example, or point me to location where I could read more about that. Thanks again!

Uros
 
Sorry I forgot to mention this. I need this for WPF application and for Windows service.
 
Code ought to be the same.
If you use Invoke (or BeginInvoke) it ought to run on a thread from the threadpool, and so get it off the thread that received the data.

Chip H.


____________________________________________________________________
www.chipholland.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top