I find myself doing this sort of thing quite often:-
EventHandler eh = null; //can\'t assign lambda directly since it uses eh
eh = (s, args) =>
{
//s
You could attache a permanent event handler to the event. The event handler then invokes "one shot event handlers" that are added to an internal queue:
OneShotHandlerQueue queue = new OneShotHandlerQueue();
Test test = new Test();
// attach permanent event handler
test.Done += queue.Handle;
// add a "one shot" event handler
queue.Add((sender, e) => Console.WriteLine(e));
test.Start();
// add another "one shot" event handler
queue.Add((sender, e) => Console.WriteLine(e));
test.Start();
Code:
class OneShotHandlerQueue where TEventArgs : EventArgs {
private ConcurrentQueue> queue;
public OneShotHandlerQueue() {
this.queue = new ConcurrentQueue>();
}
public void Handle(object sender, TEventArgs e) {
EventHandler handler;
if (this.queue.TryDequeue(out handler) && (handler != null))
handler(sender, e);
}
public void Add(EventHandler handler) {
this.queue.Enqueue(handler);
}
}
Test class:
class Test {
public event EventHandler Done;
public void Start() {
this.OnDone(new EventArgs());
}
protected virtual void OnDone(EventArgs e) {
EventHandler handler = this.Done;
if (handler != null)
handler(this, e);
}
}