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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.