简体   繁体   English

EnableRaisingEvents(启用和禁用它)

[英]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). 我正在维护一些具有两个FileSystemWatcher事件的代码,这些事件使调试变得很困难(并且有一个错误)。 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. 从主要方面,我可以将EnableRaisingEvents = true设置为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? 我的问题是这样的: 如果事件当前正在执行,并且我将EnableRaisingEvents = false设置为false,它将暂停还是继续执行直到完成?

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. 如果它确实继续,我认为只是在事件的开始和结束时设置一个bool doRUN变量,作为对main方法的检查。

You should just detach the event handler after you check to make sure that it is working properly and then instantiate the second FileSystemWatcher . 在检查以确保其正常工作之后,您应该仅分离事件处理程序,然后实例化第二个FileSystemWatcher

Inside of the OnMessageReceived you could od something like 在OnMessageReceived内部,您可能会遇到类似

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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