I'm wondering what the 'best practice' is, when asking an event handler to unsubscribe its self after firing once.
For context, this is my situation. A user is logged in, and is in a ready state to handle work items. They receive a work item, process it, then go back to ready again. At this point, they may want to say they're not available for more work items, but one is sent to them anyway. What I want to be able to do, is allow the user to 'queue' a 'I'm not available' as soon as that operation becomes possible.
public event SomeHandler StateChanged = delegate {};
public void QueueNotAvailable()
{
StateChanged += (s,e) => {
if (e.CanGoNotAvailable) {
someObject.NotAvailable();
StateChanged -= {thishandler};
}
}
}
For my purposes, if a separate thread happens to fire off the events and this particular handler runs twice, it is not a problem. Requesting the same function very close together is acceptable. I just don't want it firing every single time the operation is allowed.