A problem about FileSystemWathcher(BUG???)

G

Guest

Sir:
I have used FileSystemWathcher(.NET class) to monitor file change
envent.The code as list:

......................

int i=0; //Counter for Changed event after FileCreated event
private void IntializeFileSystemWatcher()
{
//Create File System Watcher in mypath for new files
fsWatcher=new System.IO.FileSystemWatcher(mypath,"*.*");

// Add event handlers for new files and change of existing files.
fsWatcher.Changed += new FileSystemEventHandler(OnFileChanged);
fsWatcher.Created += new FileSystemEventHandler(OnFileCreated);

// Begin watching.
fsWatcher.EnableRaisingEvents = true;

}

private void OnFileChanged(object source, FileSystemEventArgs e)
{
i++;
}

/// <summary>
/// Event Handler for File Created
///
/// </summary>
private void OnFileCreated(object source, FileSystemEventArgs e)
{
i=0;
Thread thread=new Thread(new ThreadStart(this.StartListen));
thread.Start();
}

//Wait for Changed event fired,But only get once.
void StartListen()
{
WaitForChangedResult
r=fsWatcher.WaitForChanged(WatcherChangeTypes.Changed);
bool b=r.TimedOut;
}
......................

When I break at "bool b=r.TiedOut" this line,I got only one Changed
event.But when I check i value,it is more then 1.
Why????
inexplicability!!!!
Is there something wrong? Can you help me to explain this situation?
Thank You!!!
 
L

Laura T.

If you have a lot of changes in a small time, you might get just one
notification.

From
http://msdn2.microsoft.com/en-us/library/system.io.filesystemwatcher.internalbuffersize(VS.80).aspx

"The Windows operating system notifies your component of file changes in a
buffer created by the FileSystemWatcher. If there are many changes in a
short time, the buffer can overflow. This causes the component to lose track
of changes in the directory, and it will only provide blanket notification."

Laura
 
G

Guest

You are right about event buffer.

My code can capture more then one changed event notify. But use
WaitForChanged() API,I can only capture 1 changed event,where are others? I
want to know underground processing about event notify, maybe I can get
explain with more reason.



“Laura T.â€ç¼–写:
 

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