Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing C# Events after execution?

Tags:

c#

events

Say I have the following code:

public event EventHandler DatabaseInitialized = delegate {};

//a intederminant amount of subscribers have subscribed to this event...


// sometime later fire the event, then create a new event handler...
DatabaseInitialized(null,EventArgs.Empty);

//THIS LINE IS IN QUESTION    
DatabaseInitialized = delegate {};

Will this clear out the subscribers, replacing it with new empty default? And, will the event notify all subscribers before they get cleared out? I.E. Is there a chance for a race condition?

like image 664
Alan Avatar asked Sep 22 '11 18:09

Alan


1 Answers

Yes it will clear it out. And because events fire syncronously in the same thread there shouldn't be race condition.

My advice: when in doubt, write a small test application and... well, test it.

UPDATE: I tested it before posting. (In response to minuses.)

like image 187
Dmitry Avatar answered Sep 30 '22 07:09

Dmitry