2011-09-02 271 views
1

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

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

从main中,我可以将EnableRaisingEvents = true设置为EnableRaisingEvents = false。这两个事件都将某些文件夹中的文件编入索引,并制定回调方法。

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

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

+0

它继续。小心使用bool标志,FSW使用线程来提升事件。正确的同步需要锁和/或等待手柄。再次,如果这只是为了调试,那么不要太担心它。还要考虑线程是你追逐的错误的原因。 –

回答

1

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

里面的OnMessageReceived的,你可以OD像

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