Events And Multiple Threads

G

Guest

I'm looking to understand the way events work across multiple threads. I have an object that needs to process data as it comes in. When a certain threshold is hit, it needs to tell the host application via an event. If the app is running on Thread A and the process inside the data processing object is running on Thread B, which thread runs the event handler? What's blocked and in what order? Thanks.

Jerry
 
N

Nicholas Paldino [.NET/C# MVP]

Jerry,

Event handlers are executed on the thread that fires the events. So if
you do something to fire an event, all of the event handlers are going to
execute on that thread.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I'm looking to understand the way events work across multiple threads. I
have an object that needs to process data as it comes in. When a certain
threshold is hit, it needs to tell the host application via an event. If
the app is running on Thread A and the process inside the data processing
object is running on Thread B, which thread runs the event handler? What's
blocked and in what order? Thanks.

Jerry
 
C

Chris Mullins

I'm looking to understand the way events work across multiple threads.

There's a real simple answer to that: Events don't cross threads. Any
registered handlers will run on the thread that raised the event.
 
G

Guest

Thanks guys.
I'm looking to understand the way events work across multiple threads. I have an object that needs to process data as it comes in. When a certain threshold is hit, it needs to tell the host application via an event. If the app is running on Thread A and the process inside the data processing object is running on Thread B, which thread runs the event handler? What's blocked and in what order? Thanks.

Jerry
 
J

Jeffrey Tan[MSFT]

Hi Jerry,

In addition to all other community's correct replies, if you are
programming .Net winform, I want to share another information to you.

Since winform controls are using STA threading model, if a worker(non-GUI)
thread fired an event, this event will execute in this non-GUI thread, any
accessing to the GUI control methods/properties must be marshaled with
Control.Invoke method(see this method in MSDN) to keep thread-safe. This is
Net winform multithreading programming rule and is prone to programming
error.

Please refer to "Wrapping Control.Invoke" section in the article below for
more information:
http://msdn.microsoft.com/msdnmag/issues/03/02/Multithreading/

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Top