title: Using a FileWatcher in a Web Service
body: <p>I have a client that invokes a [web method].  In the [web method] I am checking for the existence of certain files which is in itself contained in a while(true) loop. When there are files present or a time-out occurs the callback is returned to the client.</p>
  
  <p>I have noticed running it his was the wp3 process climbs in memory usage.</p>
  
  <p>Someone told me that if use a FileWatcher instead of a while(true) loop that the memory is placed on the .Net framework instead of the IIS process. I was trying to test this but I could not see how to return the callback to the client when a file is 'found'.</p>
  
  <p>My code:</p>
  
  <p><strong>[Desktop App]</strong></p>
  
  <pre><code>private void _tmrRequestHandler_Tick(object sender, EventArgs e)
  {
  try
  {
  _tmrRequestHandler.enabled = false;
  //call my web service async
  }
  catch
  {
  _tmrRequestHandler.enabled = true;
  }
  }
  
  private void WSconnector_GetRequestsCompleted(object sender, wsConnector.GetRequestsCompletedEventArgs e)
  {
  //do stuff
  _tmrRequestHandler.enabled = true;
  }
  </code></pre>
  
  <p><strong>[web server]</strong>   - the old way</p>
  
  <pre><code>[WebMethod]
  public string[] GetRequests(string _mac)
  {
     string[] _response = null;
     while (_fileCount == 0)
     {
      string[] _files = Directory.GetFiles("my root path" + _mac, "*.dat");
      _fileCount = _files.Length;
      if (_files.Length &gt; 0)
      {
       _response = new string[_files.Length];
       _files.CopyTo(_response, 0);
       return _response;
      }
   }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>[web server]</strong>   - the proposed new way</p>
  
  <pre><code>[WebMethod]
  public string[] GetRequests(string _mac)
  {
      string[] _response = null;
      FileSystemWatcher watcher = new FileSystemWatcher();
      watcher.Path = AbsoluteRequestQueue + _mac;
      watcher.NotifyFilter = NotifyFilters.LastWrite;
      watcher.Filter = "*.dat*";
      watcher.Changed += new FileSystemEventHandler(OnChanged);
      watcher.EnableRaisingEvents = true;
  }
  
  private void OnChanged(object source, FileSystemEventArgs e)
  {
      //file found!!
      //HOW DO I GIVE CALLBACK TO MY CLIENT AND SHOULD I REALLY BE CONSIDEREING DOING IT THIS WAY??
  }
  </code></pre>
  
  <p>Thanks</p>