FileSystemWatcher Changed event is raised twice

前端 未结 30 3830
死守一世寂寞
死守一世寂寞 2020-11-22 06:01

I have an application where I am looking for a text file and if there are any changes made to the file I am using the OnChanged eventhandler to handle the event

30条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 06:30

    Sorry for the grave dig, but I've been battling this issue for a while now and finally came up with a way to handle these multiple fired events. I would like to thank everyone in this thread as I have used it in many references when battling this issue.

    Here is my complete code. It uses a dictionary to track the date and time of the last write of the file. It compares that value, and if it is the same, it suppresses the events. It then sets the value after starting the new thread.

    using System.Threading; // used for backgroundworker
    using System.Diagnostics; // used for file information
    private static IDictionary fileModifiedTable = new Dictionary(); // used to keep track of our changed events
    
    private void fswFileWatch_Changed( object sender, FileSystemEventArgs e )
        {
            try
            {
               //check if we already have this value in our dictionary.
                if ( fileModifiedTable.TryGetValue( e.FullPath, out sEmpty ) )
                {              
                    //compare timestamps      
                    if ( fileModifiedTable[ e.FullPath ] != File.GetLastWriteTime( e.FullPath ).ToString() )
                    {        
                        //lock the table                
                        lock ( fileModifiedTable )
                        {
                            //make sure our file is still valid
                            if ( File.Exists( e.FullPath ) )
                            {                               
                                // create a new background worker to do our task while the main thread stays awake. Also give it do work and work completed handlers
                                BackgroundWorker newThreadWork = new BackgroundWorker();
                                newThreadWork.DoWork += new DoWorkEventHandler( bgwNewThread_DoWork );
                                newThreadWork.RunWorkerCompleted += new RunWorkerCompletedEventHandler( bgwNewThread_RunWorkerCompleted );
    
                                // capture the path
                                string eventFilePath = e.FullPath;
                                List arguments = new List();
    
                                // add arguments to pass to the background worker
                                arguments.Add( eventFilePath );
                                arguments.Add( newEvent.File_Modified );
    
                                // start the new thread with the arguments
                                newThreadWork.RunWorkerAsync( arguments );
    
                                fileModifiedTable[ e.FullPath ] = File.GetLastWriteTime( e.FullPath ).ToString(); //update the modified table with the new timestamp of the file.
                                FILE_MODIFIED_FLAG.WaitOne(); // wait for the modified thread to complete before firing the next thread in the event multiple threads are being worked on.
                            }
                        }
                    }
                }
            }
            catch ( IOException IOExcept )
            {
                //catch any errors
                postError( IOExcept, "fswFileWatch_Changed" );
            }
        }
    
        

    提交回复
    热议问题