简体   繁体   English

C#-File Watcher作为Windows服务监视多个目录

[英]C# - File Watcher as a windows service watching multiple directories

I have a problem that I can't seem to find a solution for. 我有一个似乎无法找到解决方案的问题。 I am writing a file watcher Windows Service in C# using FileSystemWatcher. 我正在使用FileSystemWatcher用C#编写文件监视程序Windows服务。 I would like to watch multiple independent directories, I have the directories to watch and some other settings in a xml file. 我想观看多个独立目录,我在XML文件中有要观看的目录和其他一些设置。 The program reads in the xml, creates the appropriate number of FileSystemWatchers and stores these system watchers in a dictionary with a randomly generated id number (as the keys). 该程序读取xml,创建适当数量的FileSystemWatchers,并将这些系统监视程序存储在具有随机生成的ID号(作为键)的字典中。 The problem that I am having is that only the first FileSystemWatcher is responding to files being created/deleted/updated ect. 我遇到的问题是,只有第一个FileSystemWatcher才能响应正在创建/删除/更新的文件。 I know the others have been create because the entries are in the dictionary. 我知道其他人已经创建,因为条目在字典中。 I know EnableRaisingEvents = true because all of the FileSystemWatcher are created the same way via a loop. 我知道EnableRaisingEvents = true,因为所有FileSystemWatcher都是通过循环以相同的方式创建的。 I have no idea how to get multiple file watchers working correctly, and I would like to avoid threads if possible. 我不知道如何使多个文件监视程序正常工作,如果可能,我想避免使用线程。 If anyone has any ideas or resources to look at, that would be greatly appreciated. 如果任何人有任何想法或资源可供参考,将不胜感激。

Im afraight you háve to instantiate some kind of thread pool or task factory because a filewatcher can only watch one file in a thread. 恐怕您必须实例化某种线程池或任务工厂,因为文件监视程序只能监视线程中的一个文件。

However this posting has a very nice example of doing so: How to assign separate thread for each File System Watcher? 但是,此发布有一个很好的示例: 如何为每个File System Watcher分配单独的线程?

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM