title: Windows service with FileSystemWatcher and Timer - making sure everything gets disposed
body: <p>I have created a C# Windows Service application that starts a <code>FileSystemWatcher</code> to monitor a directory for the creation of a file.  When the file is found I instantiate a custom class that parses the file (CSV) and calls a web service with it's contents.  The service is somewhat asynchronous and returns a unique number which must be used for subsequent calls to check its progress.  In my process class I create a timer to continually check to see if the job is finished.  I am <code>dispose</code>ing and <code>close</code>ing my timer when I am done with it but I just want to make sure my class will be garbage collected and I will not have any memory leaks.</p>
  
  <p>The code looks like this (snipped for brevity):</p>
  
  <p><strong>My main service class:</strong></p>
  
  <pre><code>    protected override void OnStart(string[] args)
      {
          FileSystemWatcher watcher = new FileSystemWatcher();
          watcher.Path = "path";
          watcher.Filter = "file";
          watcher.Created += new FileSystemEventHandler(watcher_Created);
          watcher.EnableRaisingEvents = true;
      }
  
      static void watcher_Created(object sender, FileSystemEventArgs e)
      {
          FileProcessor p = new FileProcessor();
          p.Process(e.FullPath);
  
          //Will this instance of p stick around until the timer within it is finished?
      }
  </code></pre>
  
  <p><strong>FileProcessor.cs</strong></p>
  
  <pre><code>    class FileProcessor
      {
          private System.Timers.Timer timer = new System.Timers.Timer();
          private string id;
  
          public FileProcessor()
          {
              timer.Elapsed += new ElapsedEventHandler(OnTimer);
              timer.Enabled = false;
              timer.AutoReset = true;
          }
  
          public void Process(string filename)
          {
              //Read file &lt;snipped&gt;
  
              //Call web service and get id
              id = CallWebService();
  
              //Create a timer for 10 seconds and start it
              timer.Interval = 10000;
              timer.Enabled = true;
          }
  
  
          private bool IsFinished(string id)
          {
              //Call web service to see if job is finished, true if finished
              //&lt;snipped&gt;
          }
  
  
          private void ProcessResults()
          {
              //Job is finished, process results
  
              //Call cleanup method to dispose of timer
              Cleanup();
          }
  
          private void OnTimer(object source, ElapsedEventArgs e)
          {
              if (!IsFinished(id))
              {
                  //Keep timer running, check result again next timer event
                  return;
              }
              else
              {
                  //Stop timer
                  timer.Stop();
  
                  //Process Results
                  ProcessResults(response);
              }
          }
  
  
          private void Cleanup()
          {
              timer.Close();
              timer.Dispose();
          }
      }
  </code></pre>
  
  <p>My question is should my instance of "p" stick around (not be GC'ed) until my timer is destroyed?  Will it ever be destroyed?  Does my FileProcessor class need to implement IDisposable so I can wrap it in a using block?  I am not worried about this being single threaded because I only expect it to process one file per day and it should not take more than 10 minutes for the process to finish and return to watching for the next file to be created.</p>