0

There is a small event handler example at msdn

with the line:

myNewLog.EntryWritten += new EntryWrittenEventHandler(MyOnEntryWritten);

Presumably this adds the triggering event to a queue to be handled. What removes the handler from the queue? Do I even need to think about this?

Kal
  • 378
  • 3
  • 15
  • I changed the logic so that line is only written once rather than being inside the loop. There is no longer a need to remove the event from the queue. – Kal Apr 08 '13 at 18:14

1 Answers1

3

The "-=" operator removes the subscriber from the publisher. Not unsubscribing is a problem when the publisher of the event will live longer than the subscriber.

More info here and here.

Community
  • 1
  • 1
  • So in the referenced example I would add a -= entry, otherwise identical to the += version, immediately above the Signal.Set(); line? In my case this will be part of a Windows Service that could keep running for a long time, while the handler will finish in a second or so. – Kal Apr 08 '13 at 05:58
  • @Kal: no, `myNewLog` is on a different scope so you don't have access to it inside `MyOnEntryWritten`. It would make sense to unsubscribe after `signal.WaitOne()` (if you want to set the `AutoResetEvent` to a [signaled state](http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx) only once). – Victor Lacorte Apr 08 '13 at 12:29
  • Since I want the event to happen repeatedly, are you saying I should not set the -= at all? That kind of brings the question back full circle. – Kal Apr 08 '13 at 15:16