Re: .NET Base Class Library FileSystemWatcher created event fires too early (multitask)
xeondev
Sorry Peter my post was a bit confusing, but i try again.
I have a simple console app.
class Program
{
static void Main(string[] args)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"c:\source";
watcher.Filter = "*.*";
watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
watcher.IncludeSubdirectories = false;
watcher.Created += new FileSystemEventHandler(watcher_Created);
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.EnableRaisingEvents = true;
Console.WriteLine("Listening started");
Console.ReadLine();
}
static void watcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("Changed {0}", e.FullPath);
}
static void watcher_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine("Created {0}", e.FullPath);
}
}
If i drop some files in the source folder the result will be similar like this:
Listening started
Created c:\source\nyomt.JPG
Changed c:\source\nyomt.JPG
Changed c:\source\nyomt.JPG
Changed c:\source\nyomt.JPG
Created c:\source\irany.JPG
Changed c:\source\irany.JPG
Changed c:\source\irany.JPG
Changed c:\source\irany.JPG
Created c:\source\krtipus.JPG
Changed c:\source\krtipus.JPG
Changed c:\source\krtipus.JPG
Changed c:\source\krtipus.JPG
I suppose if a slow process is generating "nyomt.JPG" and the other processes are faster
i could end up in the following result
Listening started
Created c:\source\nyomt.JPG
Changed c:\source\nyomt.JPG
Changed c:\source\nyomt.JPG
Changed c:\source\nyomt.JPG
Created c:\source\irany.JPG
Changed c:\source\irany.JPG
Created c:\source\krtipus.JPG
Changed c:\source\krtipus.JPG
Changed c:\source\nyomt.JPG
Changed c:\source\nyomt.JPG
Changed c:\source\nyomt.JPG
Changed c:\source\nyomt.JPG
My real goal is to get to know when the creating is finished. The created event fires when the
creation is started. Other thread was also discussing this:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=603441&SiteID=1
scottwis_MS
said:
"I
would recommend that you use a timer component in conjunction with the
FileSystemWatcher component, setting the time to something around 1
second or so. When you receive the "created" event you can then start
the timer and then start listing to "changed" events from the
FileSystemWatcher. Every time you get a changed event you can then
reset the timer. Once the timer fires, you can then go ahead and do
your processing."
I suppose a simple timer is not enough in the second "mixed" scenario, it needs somehow to track the events and filenames but i'm running out of ideas.
Regards
xeondev