Strange behaviour when raising an event in FileSystemWatcher eventhandler......

A

Asko Telinen

Hi all.

I ran into quite strange problem concerning the event raising inside
FileSystemWatcher Delete event.

First, i would like to describe a bit my environment.

I have main GUI application, which uses other class libraries.
One lib, called Utils.dll contains custom collection implementation.
This simple collection just overrides Add and Remove methods to raise
events whenever item is added or removed from collection.

Another one, called DirStruct.dll, is responsible for watching
filesystem operations in certain directory and update the collection
items to reflect filesystem changes. To achieve that, i used
FileSystemWatcher object and assigned Created/Deleted event handlers.

The collections Add and Remove methods in Utils.dll look like this (more
or less):

protected override void CMyCollection::Add(DMFile file) {
base.Add(file.Name); // Throws an exception if exists
if ( OnFileAdded != null ) {
OnFileAdded(this, file);
}
}

protected override void CMyCollection::Remove(DMFile file) {
if ( base.Remove(file.Name) && OnFileRemoved != null ) {
OnFileRemoved(this, file);
}
}

Here is CMyCollection is inherited from custom template class,
ItemCollection<TValue> and ItemCollection inherits from
Dictionary<TKey, TValue>

class CMyCollection : ItemCollection<DMFile>

class ItemCollection<TValue> : Dictionary<string, TValue> where TValue:
KeyItemBase

Kinda messy :)


So far so good. Now, when the DirStruct object receives a filesystem
Create or Delete notification, it calls those methods respectively and
everything works just fine EXCEPT, the Remove method in MY class will
never called. Even though item is removed from collection. On the
contrary, Add method is called normally, OnFileAdded event will be
raised and application GUI will eventually be updated(using Invoke).
When i tried to step into the Remove method in debugger, it never will,
just jumps over. As far as i can see, these two methods in colledction
class should be executed in same context but seems like they don´t. I
know the FileSystemWatcher event handlers are executed in different
thread but it doesn´t matter cause there is no GUI in collection to update.

What i´m missing here?

thanx


Asko.
 
S

Samuel R. Neff

I would suggest saving the result of base.Remove(file.Name) to a local
variable so you can inspect it in the debugger and then be absolutely
sure that it's return true as you suspect.

bool removed = base.Remove(file.Name);
if (removed && OnFileRemoved != null) {
OnFileRemoved(this, file);
}

HTH,

Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top