C#filesystemwatcher会在一段时间后停止触发事件 [英] C# filesystemwatcher stops firing events after sometime

查看:372
本文介绍了C#filesystemwatcher会在一段时间后停止触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想跟踪特定路径的文件更改,我已经完成了现在工作正常的代码。它正在跟踪文件创建,重命名和更改。



我的问题是当我启动Filesystemwatcher它工作正常但经过一段时间它停止工作,即它停止触发创建,删除和更改事件。



任何人都可以帮助我?



提前谢谢。



这是我的代码lstFolder是我的多条路径list



i我正在使用窗口应用程序来查看文件,我正在使用窗口服务启动该窗口应用程序。



I want to track file changes of particular path and I am pretty much done with the code which is now working fine.it is tracking file creation , renamed and changed .

My problem is when I am launching Filesystemwatcher it's working fine but after some time its stop working i.e it stops firing creation ,deleted and changed event.

Can anybody help me out?

Thank you in advance.

Here is my code lstFolder is my multiple path list

i am using window application to watch files and i am using window service to start that window app.

this.listFileSystemWatcher = new List();

            // Loop the list to process each of the folder specifications found
            if (lstFolder.Count > 0)// check if path is available to watch else exit file watcher
            {
                foreach (CustomFolderSettings customFolder in lstFolder)
                {
                    DirectoryInfo dir = new DirectoryInfo(customFolder.FWPath);
                    // Checks whether the folder is enabled and
                    // also the directory is a valid location
                    if (dir.Exists)//customFolder.FolderEnabled && 
                    {
                        customFolder.AllowedFiles = customFolder.FWExtension;// setting extension to allowed filw extension to log .
                        foreach (var strExt in customFolder.FWExtension.Split(','))
                        {

                            // Creates a new instance of FileSystemWatcher
                            //FileSystemWatcher fileSWatch = new FileSystemWatcher();
                             this.fileSWatch = new FileSystemWatcher();
                            // Sets the filter
                            fileSWatch.Filter = strExt;// customFolder.FolderFilter;
                                                       // Sets the folder location
                            fileSWatch.Path = customFolder.FWPath;
                            fileSWatch.InternalBufferSize = 64000;
                            // Sets the action to be executed
                            StringBuilder actionToExecute = new StringBuilder(customFolder.ExecutableFile);
                            // List of arguments
                            StringBuilder actionArguments = new StringBuilder(customFolder.ExecutableArguments);
                            // Subscribe to notify filters
                            fileSWatch.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
                            // Associate the events that will be triggered when a new file Created,Changed,Deleted,Renamed //
                            // is added to the monitored folder, using a lambda expression                   
                            fileSWatch.Created += (senderObj, fileSysArgs) => fileSWatch_Created(senderObj, fileSysArgs, actionToExecute.ToString(), customFolder.AllowedFiles);
                            fileSWatch.Changed += (senderObj, fileSysArgs) => fileSWatch_Changed(senderObj, fileSysArgs, actionToExecute.ToString(), customFolder.AllowedFiles);

                            fileSWatch.Deleted += (senderObj, fileSysArgs) => fileSWatch_Deleted(senderObj, fileSysArgs, actionToExecute.ToString(), customFolder.AllowedFiles);
                            fileSWatch.Renamed += (senderObj, fileSysArgs) => fileSWatch_Renamed(senderObj, fileSysArgs, actionToExecute.ToString(), customFolder.AllowedFiles);
                            fileSWatch.Error += (senderObj, fileSysArgs) => fileSWatch_Error(senderObj, fileSysArgs, actionToExecute.ToString(), customFolder.AllowedFiles);


                            // will track changes in sub-folders as well
                            fileSWatch.IncludeSubdirectories = customFolder.FWSubFolders;
                            // Begin watching
                            fileSWatch.EnableRaisingEvents = true;


                            // Add the systemWatcher to the list
                            listFileSystemWatcher.Add(fileSWatch);
                            GC.KeepAlive(fileSWatch);
                            GC.KeepAlive(listFileSystemWatcher);
                        }

                    }
                }

            }
            else
            {
                Application.Exit();
            }





我的尝试:



尝试使用内部缓冲存储和GC.keepalive()但问题仍然存在。



What I have tried:

tried using internal buffer storage and GC.keepalive() but problems remains same.

推荐答案

我怀疑你的 FileSystemWatcher (或在FSW事件中运行的代码)抛出异常,而你却没有抓住它。
I suspect that your FileSystemWatcher (or code that runs in a FSW event) is throwing an exception, and you're not catching it.


这篇关于C#filesystemwatcher会在一段时间后停止触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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