Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am writing a multi threaded application and I want raise events such that if event happens in thread 1, it is notified to other threads.

for eg: In my application I have 4 threads (Thread A, Thread B, Thread C, Thread D). All four Threads are started during the start up of the application. Thread B and Thread C need to know if a particular event happens in Thread A. How can Thread A inform Thread B and Thread C if that event occurs?

Thanks in advance.
Posted

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Feb-15 19:21pm    
Sorry, this can helpful, but no more helpful than adding similar link on event topics. No way it addresses the inquirer's concern. Please see Solution 3 for some more detail.
—SA
There is no such mechanism. It's not absolutely obvious that such mechanism should be added to the framework; I would rather call it a discussable topic. You have to create it if you really need it. Events are pretty much unrelated to threading. You just need to learn both and combine the way you want.

But now, be careful. Many will maintain that such mechanism does exist: Control.Invoke, Control.BeginInvoke, Dispatcher.Invoke, Dispatcher.BeginInvoke, System.Collections.Concurrent.BlockingCollection, task cancellation tokens, System.Threading.WaitHandle and derived classes, and more. All those mechanism are just not what you are asking about, but have a lot in common. They are just not related to what is called ".NET events". Moreover, you can use some of those mechanisms, in combination with .NET events or not, to achieve desired behavior. This is a matter of your technology architecture.

You need to realize only on fundamental fact here: you cannot invoke execution of any code in any arbitrary thread from outside of it. No matter what you do, you have to implement some collaborative behavior on target thread: it needs to run some cycle with message queue (as UI threads do, which makes invocation possible), or something like that. We could discuss some approaches, but first you need to get into it and get to more specific detail.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900