One shot events using Lambda in C#

前端 未结 8 1695
無奈伤痛
無奈伤痛 2021-01-31 07:55

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         


        
8条回答
  •  萌比男神i
    2021-01-31 08:19

    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);
        }
    }
    

提交回复
热议问题