c# - 如何同步 FileSystemWatcher

标签 c# multithreading

我有这段代码 我想在插件的 StartWatch() 和 OnFinalChanged() 之间同步,所以最终将按以下顺序打印:

Creating plugin for file c:\temp\file1.txt
Creating plugin for file c:\temp\file2.txt
Creating plugin for file c:\temp\file3.txt
Starting watching c:\temp\file1.txt
Finished watching file c:\temp\file1.txt
Starting watching c:\temp\file2.txt
Finished watching file c:\temp\file2.txt
Starting watching c:\temp\file3.txt
Finished watching file c:\temp\file3.txt
namespace Test2
{
    internal static class MyProgram
    {
        [STAThread]
        private static void Main(string[] args)
        {
            List<Plugin> plugins = new List<Plugin>
                                       {
                                           new Plugin(@"c:\temp\file1.txt"),
                                           new Plugin(@"c:\temp\file2.txt"),
                                           new Plugin(@"c:\temp\file3.txt"),
                                       };

            foreach (var plugin in plugins)
            {
                // I need StartWatch to be called only after i got OnChanged() for the previous plugin
                plugin.StartWatch();
            }
        }
    }


    public class Plugin
    {
        private FileSystemWatcher _watcher;
        private readonly string _file;

        public Plugin(string file)
        {
            _file = file;
            Console.WriteLine("Creating plugin for file {0}", _file);
        }

        public void StartWatch()
        {
            Console.WriteLine("Starting watching {0}", _file);
            FileInfo fileInfo = new FileInfo(_file);
            if (fileInfo.Directory != null)
            {
                _watcher = new FileSystemWatcher(fileInfo.Directory.FullName, fileInfo.Name);
                _watcher.NotifyFilter = NotifyFilters.LastWrite;
                _watcher.Changed += OnChanged;
                _watcher.EnableRaisingEvents = true;
            }
            // i want the main thread to be paused untill i get the OnChanged()
        }

        private void OnChanged(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("Finished watching file {0}", _file);
            _watcher.Dispose();

            // once i get this event, i want the main thread to continue and to start watching the same plugin
        }
    }
}

最佳答案

使用 AutoResetEvent应该完成这项工作。

namespace Test2
{
    internal static class MyProgram
    {
        [STAThread]
        private static void Main(string[] args)
        {
            List<Plugin> plugins = new List<Plugin>
                                       {
                                           new Plugin(@"c:\temp\file1.txt"),
                                           new Plugin(@"c:\temp\file2.txt"),
                                           new Plugin(@"c:\temp\file3.txt"),
                                       };

            foreach (var plugin in plugins)
            {
                // I need StartWatch to be called only after i got OnChanged() for the previous plugin
                plugin.StartWatch();
                plugin.WaitHandler.WaitOne();
            }
        }
    }


    public class Plugin
    {
        private FileSystemWatcher _watcher;
        private readonly string _file;
        public AutoResetEvent WaitHandler {get;private set;} 

        public Plugin(string file)
        {
            _file = file;
            Console.WriteLine("Creating plugin for file {0}", _file);
            WaitHandler = new AutoResetEvent(false);
        }

        public void StartWatch()
        {
            Console.WriteLine("Starting watching {0}", _file);
            WaitHandler.Reset();
            FileInfo fileInfo = new FileInfo(_file);
            if (fileInfo.Directory != null)
            {
                _watcher = new FileSystemWatcher(fileInfo.Directory.FullName, fileInfo.Name);
                _watcher.NotifyFilter = NotifyFilters.LastWrite;
                _watcher.Changed += OnChanged;
                _watcher.EnableRaisingEvents = true;
            }
            // i want the main thread to be paused untill i get the OnChanged()
        }

        private void OnChanged(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("Finished watching file {0}", _file);
            _watcher.Dispose();

            // once i get this event, i want the main thread to continue and to start watching the same plugin
            WaitHandler.Set();
        }
    }
}

关于c# - 如何同步 FileSystemWatcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9518028/

相关文章:

c# - IEnumerable.Single 引发异常

c# - Entity Framework Core 不会映射到 BigInteger

c - 线程函数内的奇怪行为

C# 在不使用 WinPcap 的情况下监听 HTTP 请求?

c# - 如何从字节数组中删除 BOM

c# - 如何将嵌套在 linq 中的相关实体获取到实体?调查问题、子问题和选项

wpf - 不能在两个线程中同时使用 Window 控件

java - 在 while 循环中检查 wait() 的什么条件?

java - Wait() 在 Java 中实际上意味着什么?我等还是你等?

java - MySQL 和 Java 中的多线程编程