Click here to Skip to main content
15,905,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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();
            }


What I have tried:

tried using internal buffer storage and GC.keepalive() but problems remains same.
Posted
Updated 2-Oct-18 2:44am
v2
Comments
[no name] 27-Sep-18 3:06am    
From what I read about GC.KeepAlive, it is a very tricky thing. GC.KeepAlive does not prevent GC from deleting an object "forever".

Have a look e.g. here:
When GC.KeepAlive Doesn’t – Chris Lyon's WebLog[^]

I would simply make sure that the scope of your listFileSystemWatcher fits.
[no name] 27-Sep-18 18:09pm    
How "many" file watchers are you creating?

Have you monitored their memory consumption over time?

How many file watchers is "too many"?

How many "events" are we talking about?

Do you ever "dispose" of a file watcher?

Do you ever "restart" a file watcher?
Dirk Bahle 28-Sep-18 7:28am    
I have read Ben's article the other day and thought his pattern is quit interesting since I have run into similar problems. I had no time to try it out for myself but maybe there is something helpful here:
https://www.codeproject.com/Articles/1220093/A-Robust-Solution-for-FileSystemWatcher-Firing-Eve

1 solution

I suspect that your FileSystemWatcher (or code that runs in a FSW event) is throwing an exception, and you're not catching it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900