Event method called twice

  • Thread starter Thread starter primem0ver
  • Start date Start date
P

primem0ver

Guest
I have an event handler that watches the file system to let me know when a file has changed and how it was changed. The event handler reports how it was changed to the screen. The problem is that it gives me the report twice. My guess is that two instances of the handler are being added to the event. (Event += hander is being called twice). How do I make sure this doesn't happen when I have no control over the event?

I tried writing an extension method:


public static bool IsRegistered(this FileSystemEventHandler handler, Delegate prospectiveHandler)
{
return handler != null && handler.GetInvocationList().Any(existingHandler => existingHandler == prospectiveHandler);
}


The problem is that the following line gives an error:

FileWatcher.Changed.IsRegistered()

Compiling (and Intellisense) say:
The event 'FileSystemWatcher.Changed' can only appear on the left hand side of += or -=

From what I have researched, you can only perform checks like this inside the class that issues the event... so has Microsoft written code to do this automatically? (and something else is causing the double report/message)? If not, how can I check if an external event has a handler already (in a way that is thread safe)?

Continue reading...
 
Back
Top