简体   繁体   中英

EnableRaisingEvents (enabling and disabling it)

I am maintaining some code which has two FileSystemWatcher events that makes it difficult to debug (and it has an error). So my idea is to simplify the code by making the execution sequential. Pretty much like this:

Main method
    1) normal code here
    2) enable event 1, let it check for files, disable it when it is done running once
    3) enable event 2, let it check for files, disable it when it is done running once

Then the database logs would make more sense. I would be able to see which part of the program that is doing something wrong.

private void InitializeFileSystemWatcher()
{
    this.MessageMonitor = new FileSystemWatcher(this.MessagePath, this.MessageFilter);
    this.MessageMonitor.IncludeSubdirectories = true; // Recursive.
    this.MessageMonitor.Created += new FileSystemEventHandler(OnMessageReceived);
    this.MessageMonitor.EnableRaisingEvents = true;
}

From the main, I can set the EnableRaisingEvents=true to EnableRaisingEvents=false. Both events indexes the files in some folder and enacts a callback method.

My question is this: If the event is currently executing and I set EnableRaisingEvents=false, will it pause or continue to execute until it finishes?

If it does continue, I figure just to have a bool doRUN variable set at beginning and the end of the event as a check for the main method.

You should just detach the event handler after you check to make sure that it is working properly and then instantiate the second FileSystemWatcher .

Inside of the OnMessageReceived you could od something like

public void OnMessageRecieved(Object sender, Events e) //Not the real signature
{
    MessageMonitor.Created -= OnMessageReceived();
    //Do Your things
    OtherMessageMonitor.Created += OnMessageReceived();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM