FileSystemWatcher-事件未第二次触发 [英] FileSystemWatcher - event not firing the second time

查看:71
本文介绍了FileSystemWatcher-事件未第二次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个启动其他应用程序的应用程序,然后等待它们创建特定的数据文件(它一次监视一个应用程序).每次启动应用程序时,它都会在特定目录中监视要创建的特定文件.我正在使用FileSystemWatcher来执行此操作(将其设置到目录中,然后筛选出正确的文件名).第一次(始终)效果很好,但是启动的第二个应用程序永远不会触发该事件.似乎触发事件的唯一方法是在事件处理程序中放置断点,或者在事件处理程序中具有Thread.Sleep命令.这对我来说似乎很奇怪...是否存在一些我不知道的比赛条件?这是代码.注意,我有一个Thread.Sleep(500).有了这一行,代码每次都能工作.没有它将会失败.我真的不习惯依靠睡眠命令.我不确定什么情况会导致它无法正常工作.

I have an application that launches other applications, and then waits for them to create a specific data file (it watches one application at a time). Each time an application is launch it watches a specific directory for a specific file to be created. I am using the FileSystemWatcher to do this (set it to the directory, then filter for the correct file name). This works great the first time (always), but the second application launched never fires the event. The only way it seems to fire the event is if I place a break-point in the event handler, or if I have a Thread.Sleep command in the event handler. This seems very strange to me...is there some race condition that I'm not aware of? Here is the code. Notice I have a Thread.Sleep(500). With this line the code works every time. Without it will fail. I'm really not comfortable relying on a Sleep command. I'm not sure what condition will cause that not to work as well.

    public static void watchFiles(string path)
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = path;
        watcher.Created += new FileSystemEventHandler(watcher_Handler);
        watcher.EnableRaisingEvents = true;
   }

    public static void watcher_Handler(object sender, FileSystemEventArgs e)
    {
        //Hack - the sleep allows the second and third application to be caught by this event
        Thread.Sleep(500);

        switch (e.ChangeType.ToString())
        {
            case "Changed":
                break;
            case "Deleted":
                break;
            case "Created":
                if (e.Name == "log.dat")
                {
                    parseDataFile();
                    moveHTMLtoLMS();

                }
                break;
            default:
                break;
        }
    }

任何人都知道为什么我需要具有Sleep(或断点)功能才能使代码再次运行吗?

Anyone know why I need to have that Sleep (or break-point) to get the code to work a second time?

推荐答案

public static void watchFiles(string path)
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = path;
    watcher.Created += new FileSystemEventHandler(watcher_Handler);
    watcher.EnableRaisingEvents = true;
}

watcher变量在此方法结束时可以进行垃圾回收.使其成为类级别成员,而不是成为局部变量:

The watcher variable is eligible for garbage collection at the end of this method. Instead of being a local variable, make it a class-level member as such:

private static FileSystemWatcher watcher;

public static void watchFiles(string path)
{
    if (watcher != null)
    {
        watcher.EnableRaisingEvents = false;
        watcher.Created -= new FileSystemEventHandler(watcher_Handler);
    }

    watcher = new FileSystemWatcher();
    watcher.Path = path;
    watcher.Created += new FileSystemEventHandler(watcher_Handler);
    watcher.EnableRaisingEvents = true;
}

这篇关于FileSystemWatcher-事件未第二次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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