-
August 8th, 2011, 11:30 AM
#1
filesystem watcher duplicates events
Hello,
I wrote a simple watcher that monitors a folder for events (files being changed - they are overwritten via ftp) and when this happens displays a message on the console and runs a matlab script.
this is part of the code:
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.Name + " " + e.ChangeType);
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "C:\\Program Files\\MATLAB\\R2011a\\bin\\matlab.bat",
Arguments = "-r \"prova=1;file\" ";
}
};
process.Start();
process.WaitForExit();
}
public static void Run()
{
string pathToWatch = "C:\\Documents and Settings\\aa";
string[] args = System.Environment.GetCommandLineArgs();
.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = pathToWatch;
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Changed += new FileSystemEventHandler(OnChanged);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
It works all fine besides the fact that every time a file is changed, it displays it twice on the console and runs matlab twice. There are ways to avoid this, of course, but I was wondering why this happens?
-
August 10th, 2011, 04:19 AM
#2
Re: filesystem watcher duplicates events
Seems to be a known issue, see: http://bytes.com/topic/c-sharp/answe...esystemwatcher
Possible workaround: http://spin.atomicobject.com/2010/07...atcher-events/
Also, please use [code] and [/code] tags around your code; it will preserve formatting and make it more readable.
Hope that helps!
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
August 10th, 2011, 10:30 PM
#3
Re: filesystem watcher duplicates events
More likely is the WaitForExit causes the OnChanged handler to block which in tern causes the change evevts to queue up.
-
August 11th, 2011, 12:46 AM
#4
Re: filesystem watcher duplicates events
Originally Posted by Arjay
More likely is the WaitForExit causes the OnChanged handler to block which in tern causes the change evevts to queue up.
Hrm... I am not sure about this. It oughtn't to trigger multiple events for any single file change, so the amount of time it takes to process doesn't seem like it should matter. The articles I referenced seemed to suggest that the duplicate events for the same file where due to FileSystemWatcher monitoring underlying filesystem events which were (for whatever reason) duplicated.
No?
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
August 11th, 2011, 12:16 PM
#5
Re: filesystem watcher duplicates events
Originally Posted by BioPhysEngr
Hrm... I am not sure about this. It oughtn't to trigger multiple events for any single file change, so the amount of time it takes to process doesn't seem like it should matter. The articles I referenced seemed to suggest that the duplicate events for the same file where due to FileSystemWatcher monitoring underlying filesystem events which were (for whatever reason) duplicated.
No?
I not saying this is definitely the problem, but if the watcher throws an event and the OnChanged event doesn't immediately return, it's possible that another event will occur while the OnChange event is blocked.
One way to verify my suggestion is to write some debug output strings inside the OnChange handler (and temporarily remove the blocking process.WaitForExit() call).
Another possibility is the OnChange handler might be used for multiple change events. If I recall correctly, there is a filter that specifies what types of events that are thrown (i.e. new, changed, renamed, etc. events). If these are wired up to the same handler, you can get what appear as duplicates if the same handler is used.
Check out the ChangeType property for the FileSystemEventArgs to help narrow down the cause of the change.
Last edited by Arjay; August 11th, 2011 at 12:21 PM.
-
August 11th, 2011, 02:07 PM
#6
Re: filesystem watcher duplicates events
I can't say for sure but am thinking that perhaps the reason it fires twice when a file is overwritten could be that it sees the existing file removed then created. I did some work with this object a while back and in some instances it was firing twice. I ended up coding around it so I could get the program out to the end user on time.
Always use [code][/code] tags when posting code.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|