pseudo-code:
class A
{
Dictionary<string, object> dic = new Dictionary<string, object>();
public Do()
{
some_a.SomeEvent += (s, e) =>
{
dic["some_string"] = new object();
};
dic["some_other_string"] = new object();
}
}
Is this safe? It would be if both dictionary operations were on single same thread. So are they?
EDIT In my situation event is fired in the same thread as Do, so it’s safe.
An event happens (usually) on the thread that invokes the event. So we can’t actually comment completely since you don’t show the code that causes the event to be invoked!
Strictly speaking, the event could be on any thread – either because a random thread is calling
OnSomeEvent(or whatever the trigger is), or if theOnSomeEventimplementation chooses to useBeginInvokefor some reason, but this is unlikely.Ultimately, it comes down to: is there a reason to think multiple threads are involved in this code.
But: what absolutely is not the case: no, there is nothing that will make that event automatically happen on the thread that subscribes it. An event subscription is just an object+method pair; no thread is nominated in the subscription.