views:

46

answers:

1

Hey,

I'm working on some event-driven code. In one of the components of my system an event sometimes comes up that I have no handler for at the moment. Is there any way to make the event "wait" until a handler is available for it?

Thanks,

PM

+3  A: 

There's no built-in mechanism in C# for queuing events and dispatching them as handlers are attached. Events are raised synchronously when some interesting activity happens ... "waiting" for a handler would require the process to block.

You could implement your own event queuing by providing a custom implementation of the event member in your class. You would need a separate data structure to cache information about previously "raised" events and queue them for when a handler attaches.

The trouble with the scheme above, is that while it is possible it is highly unusual and a bit confusing. Most developers don't expect events to be raised in such a manner. It is also complicated by the fact that if a large number of events occur without an attached handler you could end up using a significant amount of memory to store events that will never be dispatched. Personally, I would generally advise avoiding such a design.

LBushkin