One shot events using Lambda in C#

前端 未结 8 1628
無奈伤痛
無奈伤痛 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条回答
  •  后悔当初
    2021-01-31 08:17

    Another user encountered a very similar problem, and I believe the solution in that thread applies here.

    In particular, what you have is not an instance of the publish/subscribe pattern, its a message queue. Its easy enough to create your own message queue using a Queue{EventHandler}, where you dequeue events as you invoke them.

    So instead of hooking on to an event handler, your "one-shot" events should expose a method allowing clients to add an function to the message queue.

提交回复
热议问题