当前位置:  开发笔记 > 编程语言 > 正文

创建同一FileSystemWatcher的多个实例

如何解决《创建同一FileSystemWatcher的多个实例》经验,为你挑选了1个好方法。



1> srandppl..:

使用工厂方法模式.

    FileSystemWatcher MyWatcherFatory(string path, object additionalParameters)
    {
        FileSystemWatcher watcher = new FileSystemWatcher(path);
        watcher.Changed += myWatcherChangedMethod;//Attach them to the same listeners,,,
        //Set additional parameters...
        return watcher.
    }

编辑:根据您提供的信息:

    public void StartWatchers()
    {
        string[] ArrayPaths = new string[2];
        List watchers = new List();
        ArrayPaths[0] = @"K:\Daily Record Checker\Test\Test1";
        ArrayPaths[1] = @"K:\Daily Record Checker\Test\Test2";

        int i = 0;
        foreach (String String in ArrayPaths)
        {
            watcher.add(MyWatcherFatory(ArrayPaths[i]));
            i++;
        }
        //Do other stuff....
        //....
        //Start my watchers...
        foreach (FileSystemWatcher watcherin watchers )
        {
            watcher.EnableRaisingEvents = true;;
            i++;
        }

    }

    FileSystemWatcher MyWatcherFatory(string path)
    {
        FileSystemWatcher watcher = new FileSystemWatcher(path);
        watcher.Changed += Watcher_Created;
        watcher.Path = path;
        watcher.Filter = "*.csv";
        return watcher;
    }

    private void Watcher_Created(object sender, FileSystemEventArgs e)
    {
        System.Threading.Thread.Sleep(1000);
        FileInfo fileInfo = new FileInfo(e.FullPath);
        if (!IsFileLocked(fileInfo))
        {
            CheckNumberOfRecordsInFile(e.FullPath);
        }          
    }

推荐阅读
凹凸曼00威威_694
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有