FileSystemWatcher and Queues with Parallel Execution

Sometime we need a program to watch over particular Directory and then based on the Input,we need to perform some other task in parallel.
 
Here, I am providing a part of program(a window service)that uses FileSystemWacther to monitor a Directory and then based on Input perform some other task parallely.
 
Note: You should learn FileSystemWatcher before going through this Post.

OnStartis the Window Service method .You can use Console pgm or TaskScheduler also.
  1. protected override void OnStart(string[] args)    
  2. {    
  3.   
  4.     current_directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);    
  5.      
  6.     try    
  7.     {    
  8.         strDir = ConfigurationManager.AppSettings["Directory"];    
  9.         fileMask = ConfigurationManager.AppSettings["FileMask"];    
  10.         strBatfile = ConfigurationManager.AppSettings["Batch"];    
  11.         strlog = ConfigurationManager.AppSettings["Log"];    
  12.   
  13.         Task.Factory.StartNew(QueueHandler);    
  14.   
  15.         var fsw = new FileSystemWatcher();    
  16.         fsw.Created += (o, e) =>    
  17.         {    
  18.             // add a file to the queue    
  19.             filenames.Enqueue(e.FullPath);    
  20.                
  21.         };    
  22.   
  23.         fsw.Path = strDir + "\\";    
  24.         fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite    
  25.                          | NotifyFilters.FileName | NotifyFilters.DirectoryName;    
  26.         fsw.Filter = fileMask;    
  27.   
  28.         fsw.EnableRaisingEvents = true;    
  29.         fsw.Deleted += new FileSystemEventHandler(OnDeleated);    
  30.         fsw.Renamed += new RenamedEventHandler(OnRenamed);    
  31.   
  32.   
  33.         fsw.EnableRaisingEvents = true;    
  34.     }    
  35.     catch (Exception exception)    
  36.     {    
  37.         CustomException.Write(CustomException.CreateExceptionString(exception.ToString()));    
  38.     }    
  39.   
  40. }   
And now we need to provide the function that runs in parallel (QueueHandler) using Queues. 
  1. static void QueueHandler()    
  2. {    
  3.     bool run = true;    
  4.     AppDomain.CurrentDomain.DomainUnload += (s, e) =>    
  5.     {    
  6.         run = false;    
  7.         filenames.Enqueue("stop");    
  8.     };    
  9.     try    
  10.     {    
  11.         while (run)    
  12.         {    
  13.             string filename;    
  14.             if (filenames.TryDequeue(out filename) && run)    
  15.             {    
  16.                 var proc = new Process();    
  17.                 proc.StartInfo.FileName = Service1.strBatfile;   //here .exe can be added  
  18.                 proc.Start();    
  19.                ;    
  20.                 Log.getLogger("File Processed after executing batch\.exe:  Filename - :" + filename + " " + "Batch File Executed- > " + Service1.strBatfile + " at timestamp : " + DateTime.Now.ToString(), Service1.strlog);    
  21.                 proc.WaitForExit(); // this blocks until the process ends....    
  22.   
  23.             }    
  24.         }    
  25.     }    
  26.     catch (Exception exception)    
  27.     {    
  28.         CustomException.Write(CustomException.CreateExceptionString(exception.ToString()));    
  29.     }  
Note: You can write your custom exception .How you want to Log depends on you.