Reputation: 161
Lets say I have 2 threads - thread A and thread B. I register an event on thread A and invoke it from thread B. Which thread would the event be executed on?
Are there any special scenario when the above is not applicable?
Also how to make a class thread safe without using any locks.
Thanks in advance
Upvotes: 3
Views: 209
Reputation: 1503290
Unless you take some explicit action, event handlers are called on the thread that raises the event. Typically "raising an event" is just matter of invoking a delegate, which is a synchronous operation on the same thread.
Also how to make a class thread safe without using any locks.
We don't have nearly enough information to answer that, and it would be in a different question ideally. But before asking that separate question, you should really ask yourself what you mean by "thread safe" - it can mean lots of different things to different people. See Eric Lippert's blog post on the topic for more information.
(In particular if you're thinking about events, consider the possibility that one thread subscribes to or unsubscribes from an event while it's being raised - what do you need to happen?)
Upvotes: 4