EnableRaisingEvents(启用和禁用它)

2024-04-06

我正在维护一些代码,其中有两个 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,那么它会暂停还是继续执行直到完成?

如果它确实继续,我认为只需在事件的开始和结束时设置一个 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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

EnableRaisingEvents(启用和禁用它) 的相关文章

随机推荐