views:

456

answers:

1

Hi

can anyone explain to me what is happening when a instance of a class declaring a static eventhandler will hold reference to other classes that have registered intent with the event handler in question, being that they are all static would there be any contention between users sessions(I mean his/her usage of the system in a point in time) in asp.net

heres an example , extract from BlogEngine.Net

Post.Saved += new EventHandler<SavedEventArgs>(Post_Saved);

Saved is static to the class post, this event is consumed all over the show, would another user's session see the post saved event since it is static? what are the threading issues I should be aware of when declaring static events?

+4  A: 

When multiple event handlers are registered for an event they are (as far as I know) run sequentially (in the order they were attached), not simultaneously. So there shouldn't be any concurrency issues.

If the event is static then yes, all user sessions will see it as they're run in the same .NET AppDomain. (I presume by "session" you mean an ASP.NET session.)

The main thing to watch out for with static events is memory leaks. If your event handler is an instance method and you attach it to a static event then that static event now has a reference to the object on which the handler is declared, so that object and anything it references will remain in memory until either the event handler is detached or the whole AppDomain the code is running in is unloaded. Because of this you have to be careful to either detach the event handler when it's no longer needed or at least make sure your event handling class doesn't reference anything else, so the memory leak is minimal.

Evgeny