EnableRaisingEvents(启用和禁用它) [英] EnableRaisingEvents (enabling and disabling it)

查看:206
本文介绍了EnableRaisingEvents(启用和禁用它)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 主要方法
1)正常代码这里
2)启用事件1,让它检查文件,当它完成运行一次
时禁用它3)启用事件2,让它检查文件,当它完成运行一次后禁用

然后数据库日志会更有意义。我可以看到程序的哪个部分出了问题。

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

从main,我可以将EnableRaisingEvents = true设置为EnableRaisingEvents = false。两个事件都会对某些文件夹中的文件进行索引并且执行回调方法。



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



如果它继续下去,我只想在开始和结束时设置一个bool doRUN变量作为检查主要方法的事件。

解决方案

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



在OnMessageReceived内部,您可以看到像



  public void OnMessageRecieved(Object sender,Events e)//不是真正的签名
{
MessageMonitor.Created - = OnMessageReceived();
//做你的东西
OtherMessageMonitor.Created + = OnMessageReceived();
}


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

这篇关于EnableRaisingEvents(启用和禁用它)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆