FileSystemWatcher and Invoke Method
<p>I created a <code>CustomFileSystemWatcherClass</code> and whenever the <code>FileSystemWatcher</code> event occurs,  I'm calling a function with Invoke of that treeview like</p>

<pre><code>this.treeView.Invoke(....)
</code></pre>

<p>The problem is when I use <code>Invoke</code>, the <code>FileSystemWatcher</code> class is not disposing properly
but when I use </p>

<pre><code>this.treeView.BeginInvoke(....)
</code></pre>

<p>the class is disposing properly, but I wanted to use <code>Invoke</code> only for some other reasons.</p>

<p>May I know how to solve this problem?</p>

<p>this is Customfilesystemwatcher class</p>

<pre><code>  class CustomFileSystemWatcher : FileSystemWatcher, IDisposable
  {
    private Timer timer;
    private Dictionary&lt;string, DateTime&gt; recentChange;
    private int interval = 500;
    public CustomFileSystemWatcher()
      : base()
    {
      InitializeMembers();
    }

    public CustomFileSystemWatcher(string Path)
      : base(Path)
    {
      InitializeMembers();
    }

    public CustomFileSystemWatcher(string Path, string Filter)
      : base(Path, Filter)
    {
      InitializeMembers();
    }
    public new event FileSystemEventHandler Changed;
    public int Interval
    {
      get
      {
        return this.interval;
      }
      set
      {
        this.interval = value;
      }
    }

    private void InitializeMembers()
    {
      this.timer = new System.Timers.Timer();
      this.recentChange = new Dictionary&lt;string, DateTime&gt;();
      this.timer.Interval = this.interval;
      this.timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimerElapsed);
      base.Changed += new FileSystemEventHandler(this.OnChanged);
    }

    private void HasAnotherFileEventOccuredRecently(FileSystemEventArgs e)
    {
        string fileName = e.FullPath;
        if (this.recentChange.ContainsKey(fileName))
          this.recentChange[fileName] = DateTime.Now;
        else
          this.recentChange.Add(fileName, DateTime.Now);
        if (!this.timer.Enabled)
          this.timer.Start();
    }
    protected new virtual void OnChanged(FileSystemEventArgs e)
    {
      if (this.Changed != null) this.Changed(this, e);
    }

    private void OnTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
      try
      {
        if (this.recentChange.Count != 0)
          lock (this.recentChange)
          {
            foreach (string key in this.recentChange.Keys.ToArray&lt;string&gt;())
            {
              if (this.recentChange.ContainsKey(key))
                if ((DateTime.Now.Ticks -
                        (this.recentChange[key]).Ticks) &gt;=
                        new TimeSpan(0, 0, 0, 0, interval).Ticks)
                {
                  this.OnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, this.Path,
                                 key.Replace(Path + "\\", "")));
                  this.recentChange.Remove(key);
                }
            }
          }
        if (this.recentChange.Count == 0)
          this.timer.Stop();
      }
      catch (Exception ex)
      {
      }
    }

    private void OnChanged(object sender, FileSystemEventArgs e)
    {
      this.HasAnotherFileEventOccuredRecently(e);
    }
    public new void Dispose()
    {
      base.Dispose();
    }
  }
</code></pre>
