468,537 Members | 1,501 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,537 developers. It's quick & easy.

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


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<TValueand ItemCollection inherits from
Dictionary<TKey, TValue>

class CMyCollection : ItemCollection<DMFile>

class ItemCollection<TValue: Dictionary<string, TValuewhere 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.

Oct 31 '06 #1
1 1689

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
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Tue, 31 Oct 2006 15:50:14 +0200, Asko Telinen <mr*****@hotmail.com>
wrote:
>
Hi all.

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

protected override void CMyCollection::Remove(DMFile file) {
if ( base.Remove(file.Name) && OnFileRemoved != null ) {
OnFileRemoved(this, file);
}
}
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
Oct 31 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by serge calderara | last post: by
4 posts views Thread by Richard Coltrane | last post: by
26 posts views Thread by buu | last post: by
1 post views Thread by Berryl Hesh | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.