2

Please excuse me if this is redundant, however all the questions related to this seem to point in different directions, also I am new to multithreaded programming.

I have a FileSystemWatcher class in my code, which watches the created event. It looks like a created event of file system watcher starts it's own thread. So sometimes the calling thread continues it's work before the work initiated in called thread of FileSystemWatcher created event finishes. I don't want this. My workflow needs to be single-threaded, so what I want to achieve is wait for for created event to finish it's work before the calling thread gets an opportunity to resume.

pesudo code:

main() {
FileSystemWatcher fsw = new FileSystemWatcher()
fsw.Path = ini.location;
fsw.Created += new FileSystemEventHandler(OnFileCreation);
fsw.EnableRaisingEvents = true;
main_engine.processDataToFile();
main_engine.processCreatedFile();
}

void OnFileCreation(object sender, FileSystemEventArgs e) {
// do some file processing
// takes time based on size of file and whether file is locked or not etc.
}

void processDataToFile() {
// do some data processing on received data and output to a file. 
}

void processCreatedFile() {
// do not want this method to be called, unless OnFileCreation() finish it's work.
}

The reason choose to use FileSystemWatcher was because sometimes files are directly placed for processing instead of main_engine getting the data first and it works on multiple locations, so did not want to roll out a homegrown solution when FileSystemWatcher was available.

user179400
  • 97
  • 7
  • what events are you trying to monitor.. look at [FileSystemWatcher Class](http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx) here – MethodMan Jan 29 '13 at 00:12
  • @DJKRAZE: The OP says "Created." – Robert Harvey Jan 29 '13 at 00:13
  • `watcher.Created += new FileSystemEventHandler(OnChanged);` this is part of the link that I posted ...so Robert Harvey not sure what you are talking about.. – MethodMan Jan 29 '13 at 00:16
  • @DJKRAZE: He's trying to monitor the Created event. – Robert Harvey Jan 29 '13 at 00:17
  • Very closely related: http://stackoverflow.com/q/1764809 – Robert Harvey Jan 29 '13 at 00:18
  • Robert Harvey, I do not have issue with FileSystemWatcher firing multiple events or Created event being fired multiple time. The type of event is just a symptom of larger issue which is filesystemwatcher being invoked in different thread than it's calling thread. – user179400 Jan 29 '13 at 00:29
  • DJ KRAZE, I very clearly said that I am using Created Event, question is not about which event to choose. – user179400 Jan 29 '13 at 00:31

1 Answers1

1

If the event fires in the separate thread you cant make it single-threaded. because this is not your code. end of story.

however it is quite simple to wait on:

...
    me.WaitOne();
    main_engine.processCreatedFile();
}

...

void OnFileCreation(object sender, FileSystemEventArgs e) {
// do some file processing
// takes time based on size of file and whether file is locked or not etc.
...
me.Set();
}

ManualResetEventSlim me = new ManualResetEventSlim(false);
Boppity Bop
  • 9,613
  • 13
  • 72
  • 151