繁体   English   中英

EnableRaisingEvents(启用和禁用它)

[英]EnableRaisingEvents (enabling and disabling it)

我正在维护一些具有两个FileSystemWatcher事件的代码,这些事件使调试变得很困难(并且有一个错误)。 所以我的想法是通过使执行顺序化来简化代码。 几乎是这样的:

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

然后,数据库日志将更有意义。 我将能够看到程序的哪一部分做错了什么。

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;
}

从主要方面,我可以将EnableRaisingEvents = true设置为EnableRaisingEvents = false。 这两个事件都会为某个文件夹中的文件建立索引并制定一个回调方法。

我的问题是这样的: 如果事件当前正在执行,并且我将EnableRaisingEvents = false设置为false,它将暂停还是继续执行直到完成?

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

在检查以确保其正常工作之后,您应该仅分离事件处理程序,然后实例化第二个FileSystemWatcher

在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