FileSystemWatcher - FileCopy don´t start sometimes

Nov 20 2009 11:21 AM
Hi i have a very strange error. All works fine but only sometimes..... Programm Info: -------------- I have a BackupDir and one TestDir... and one Logfile..... I start the File System watcher.... then I copy something to the TestDir.... now the created-event copy the file to the BackupDir.... All created files in the TestDir are Quened and progressed in the FileProcessor class.... My problem is sometimes it copys all files and sometimes nothing sometimes it copys from 10 files only 7...... My log file says:it starts the copy method but then nothing is copied and it waits for work.....(next file....) Thank u for any help!!!! FileProcessor class: -------------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Threading; using System.Windows.Forms; namespace AndyWatcher { public class FileProcessor { private Queue workQueuePath; private Queue workQueueName; private Thread workerThread; private EventWaitHandle waitHandle; private string destination; public StreamWriter Logfile; public string set_destination { set {destination = value;} } public FileProcessor() { string Date = DateTime.Now.ToShortDateString(); string Time = DateTime.Now.ToShortTimeString(); if (!File.Exists(@"c:\AndyLog" + Date + Time.Replace(":", " ") + ".txt")) { Logfile = new StreamWriter(File.Create(@"c:\AndyLog" + Date + Time.Replace(":", " ") + ".txt")); } else { Logfile = new StreamWriter(@"c:\AndyLog" + Date + Time.Replace(":", " ") + ".txt"); } Logfile.WriteLine("Programm Start:"); //Logfile.Close(); Logfile.AutoFlush = true; workQueuePath = new Queue(); workQueueName = new Queue(); waitHandle = new AutoResetEvent(true); } #region Enquene File String public void QueueInput(FileSystemEventArgs e) { string SPath = e.FullPath; string SName = e.Name; workQueuePath.Enqueue(SPath); workQueueName.Enqueue(SName); // Initialize and start thread when first file is added if (workerThread == null) { workerThread = new Thread(new ThreadStart(Work)); workerThread.Start(); Logfile.WriteLine("Start NEW Thread......Path: "+SPath+" Name: "+SName); } // If thread is waiting then start it else if (workerThread.ThreadState == ThreadState.WaitSleepJoin) { Logfile.WriteLine("Start Waiting Thread......"); waitHandle.Set(); } } #endregion #region Progress Files private void Work() { lock (this) { while (true) { Logfile.WriteLine("Start Retriving Path"); string Path = RetrieveFilePath(); Logfile.WriteLine("Start Retriving Name"); string Name = RetrieveFileName(); if (Path != null || Name != null) { Logfile.WriteLine("Start Progress File Method.....Path: " + Path + " Name: " + Name); ProcessFile(Path, Name); } else { // If no files left to process then wait Logfile.WriteLine("Wait for Work.....Path: " + Path + " Name: " + Name); waitHandle.WaitOne(); } } } } #endregion #region Dequene File Path/Name private string RetrieveFilePath() { Logfile.WriteLine("Path_Retriving....."); if (workQueuePath.Count > 0) return workQueuePath.Dequeue(); else return null; } private string RetrieveFileName() { Logfile.WriteLine("Name_Retriving....."); if (workQueueName.Count > 0) return workQueueName.Dequeue(); else return null; } #endregion #region File Job private void ProcessFile(string Path,string Name) { try { lock (this) { if (Directory.Exists(Path)) { Logfile.WriteLine("is Directory......" + Path); Directory.CreateDirectory(destination + Name); } else if (File.Exists(Path)) { Logfile.WriteLine("is File......" + Path); CopyFile(Path, destination + Name); Logfile.WriteLine("Copy Finished (Line:110): " + Name); } } } catch { } } private void CopyFile(string source, string dest) { Logfile.WriteLine("Copy Started (Line:120)"); using (FileStream sourceStream = new FileStream(source, FileMode.Open)) { Logfile.WriteLine("1"); byte[] buffer = new byte[64 * 1024]; // Change to suitable size after testing performance Logfile.WriteLine("2"); using (FileStream destStream = new FileStream(dest, FileMode.Create)) { Logfile.WriteLine("3"); int i; while ((i = sourceStream.Read(buffer, 0, buffer.Length)) > 0) { destStream.Write(buffer, 0, i); Logfile.WriteLine("Copying...... (Line:130)"+sourceStream.Position+" von "+sourceStream.Length); //OnProgress(sourceStream.Position, sourceStream.Length); } } } } public void endstream() { Logfile.Close(); } #endregion } } LogFile: -------- Start Retriving Path Path_Retriving..... Start Retriving Name Name_Retriving..... Start Progress File Method.....Path: E:\testdir\5 - Kopie.pdf Name: 5 - Kopie.pdf is File......E:\testdir\5 - Kopie.pdf Copy Started (Line:120) Copying...... (Line:130)65536 von 358733 Copying...... (Line:130)131072 von 358733 Copying...... (Line:130)196608 von 358733 Copying...... (Line:130)262144 von 358733 Copying...... (Line:130)327680 von 358733 Copying...... (Line:130)358733 von 358733 Copy Finished (Line:110): 5 - Kopie.pdf Start Retriving Path Path_Retriving..... Start Retriving Name Name_Retriving..... Wait for Work.....Path: Name: Start Waiting Thread...... Start Retriving Path Path_Retriving..... Start Retriving Name Name_Retriving..... Start Progress File Method.....Path: E:\testdir\6 - Kopie.pdf Name: 6 - Kopie.pdf is File......E:\testdir\6 - Kopie.pdf Copy Started (Line:120)<-Here is something wrong! Start Retriving Path Path_Retriving..... Start Retriving Name Name_Retriving..... Hmm and why it posts like this?

Answers (1)