Click here to Skip to main content
15,946,316 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I want to raise an event from a worker thread to the main thread. I want the delegate that gets called by the event to be executed on the main thread. What's the simplest way of doing it in .NET?

C++
//pseudocode
void MainThread() {
   while(true) {
      // doing something
      // here I have to call something periodically to be able to get OnEvent executed
   }
}

void OnEvent() {
   // do something on event on the main thread
}

void WorkerThread() {
   while(true) {
   // if something happened, raise event to main thread
   }
}


My application is console application.
Posted
Updated 17-Apr-14 18:29pm
v2
Comments
Sergey Alexandrovich Kryukov 17-Apr-14 16:27pm    
It depends in what the main thread is. The answer depends on that, fundamentally. UI thread of WPF application? System.Windows.Forms.Application? Anything else?
—SA
gaminn 18-Apr-14 0:30am    
I updated my question. The application is console application. I don't have any UI there.

1 solution

First, let's close the problem of events. You cannot ever invoke an event from anywhere except the declaring type of that event, not even from derived class. This is an important fool-proof feature of .NET. Most likely, you mean the event declared in some .NET FCL type. If so, you have no a way to invoke it then, except the ways it is already being invoked (mouse click, and so on). And you never really need to do it. I explained it all in my past answers:
Calling an Event as a Function in C++ CLI[^],
c# networking windows form[^].

If you explain to me what you though to achieve by invoking the event, I'll tell you what you really need to do.

Now, please see my comment to the question; you should have specified what you main thread is doing. If this is a thread of a WPF application or System.Windows.Forms.Application, the answer is simple:
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

If your main thread is something different, you would need to design the thread method to implement similar behavior. Only then you will be able to delegate anything to that thread. I explained how to do it in my article: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

If you are using .NET 4.0 or later, you can use more efficient class System.Collections.Concurrent.BlockingCollection<T>:
http://msdn.microsoft.com/en-us/library/dd267312%28v=vs.110%29.aspx[^].

Note that T can be a delegate, to implement the mechanism very close to Invoke/BeginInvoke. Again, read my article referenced above, to see how to do it.

So, I think you got a 100% comprehensive answer this time, but, next time, pay attention for my comment to the question and provide more relevant information.

—SA
 
Share this answer
 
v2

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