FileSystemWatcher事件不会在Windows服务中触发 [英] FileSystemWatcher Event Does Not Fire In Windows Service

查看:89
本文介绍了FileSystemWatcher事件不会在Windows服务中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有Windows服务,其中有以下任务要执行。



[ 1]连续监视目录并在到达任何新文件时将文件移动到其他目录。



对于上述任务我正在使用FileSystemWatcher Component.However The Component当它是控制台应用程序或Windows窗体时没有任何问题可以正常工作,但是当我将其转换为Windows服务时它不起作用。不确定是什么原因我有本地管理员权限在本地运行Windows服务。这里是我试图通过服务执行的代码片段。



Hi,

I have Windows Service Which Has A following Tasks To Perform.

[1] Continuously Monitor A Directory And Upon Arrival Of Any New Files Move The File To Some Other Directory.

For The Above Task I Am Using FileSystemWatcher Component.However The Component Works Fine With No Issues When It Is Console Application Or Windows Form,However It Does Not Work When I Convert It To Windows Service.Not Sure What Is The Cause I Do Have All The Local Admin Rights To Run The Windows Service Locally. Here The Code Snippet Which I Am Trying To Execute Through Service.

protected override void OnStart(string[] args)
        {
            thrMain = new Thread(StartWatching);
            thrMain.Start();
        }

        public void StartWatching()
        {
             Logger Logger = new Logger();
            try
            {

                while (true)
                {

                    if (!Directory.Exists(sourceDir))
                        Directory.CreateDirectory(sourceDir);
                    if (!Directory.Exists(processDir))
                        Directory.CreateDirectory(processDir);

                    //Watching the source folder
                    using (FileSystemWatcher fsw = new FileSystemWatcher(sourceDir))
                    {

                        //Call fsw_Created when a created event occurs

                        fsw.Created += new FileSystemEventHandler(fsw_Created);
                        fsw.IncludeSubdirectories = true;
                        fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
                        fsw.EnableRaisingEvents = true;

                    }

                    Thread.Sleep(5000);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Onstart", "Exception in Onstart method. Error : " + ex.ToString());
            }





不确定Filewatcher是否存在特定事件的问题,如更改/创建等...



Not Sure If Filewatcher Has Issues With Specific Events Like Changed/Created Etc...

推荐答案

问题是你每次都在循环内创建FileSystemWatcher的新实例,只要使用块结束,文件系统观察器的实例就会被释放并且你的线程进入睡眠状态5秒。



请在while循环和测试之外创建FileSystemWatcher实例。



下面是更新的代码,你可以尝试

Problem is that you are creating new instance of FileSystemWatcher inside loop every time and just after creation as soon as using block ends the instance of Filesystem Watcher get disposed and your thread go to sleep for 5 seconds.

Please create FileSystemWatcher instance outside of while loop and test.

Below is updated code, you can try
public static  void StartWatching()
        {
            Logger Logger = new Logger();
            try
            {
                FileSystemWatcher fsw = new FileSystemWatcher(sourceDir);

                    if (!Directory.Exists(sourceDir))
                        Directory.CreateDirectory(sourceDir);
                    if (!Directory.Exists(processDir))
                        Directory.CreateDirectory(processDir);
                        //Call fsw_Created when a created event occurs

                        fsw.Created += new FileSystemEventHandler(fsw_Created);
                        fsw.IncludeSubdirectories = true;
                        fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
                        fsw.EnableRaisingEvents = true;
                        ManualResetEvent mResetEvent = new ManualResetEvent(false);
                        mResetEvent.WaitOne();
                    
            }
            catch (Exception ex)
            {
               Logger.Error("Onstart", "Exception in Onstart method. Error : " + ex.ToString());
            }
        }


你可以使用我的工具

智能观察者 [ ^ ]

只需开发一个移动文件的插件
you can use my tool
Smart Watcher[^]
just develop a plugin to move files


这篇关于FileSystemWatcher事件不会在Windows服务中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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