Ayush
Ayush

Reputation: 42440

Best way to implement Filewatching in my case?

So, this is roughly how my C# program looks

Main()  
 // call to method that reads XML file and loads it into a list of structure  
 // call to method that works on the structure, manpulates data etc.  
 // call to method that starts filewatching  

Filwatching_method()  
 // if any change is detected, calls method OnChange

OnChange()  
 // Action on change

If OnChange method is called i.e. a change has been detected, I want to repeat the entire process all over again. Should I call Main() from OnChange, or is there another way that is better?

Upvotes: 0

Views: 73

Answers (3)

Dan Tao
Dan Tao

Reputation: 128317

Personally, this is what I think.

  1. Yes, you should call your Main from your OnChange method, if that is in fact what you want to do (i.e., just run Main again).
  2. But to prevent uncontrolled recursion, you should ensure that the file manipulation you're performing from within Main does not itself trigger OnChange. This could be by either setting a flag or temporarily removing your Changed handler and adding it back when you're done.

Upvotes: 1

Joachim VR
Joachim VR

Reputation: 2340

Create one FileSystemWatcher object, which throws several events, you can handle every event differently.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You may take a look at the FileSystemWatcher class which allows you to monitor and be notified of events happening on the file system like file being changed.

Upvotes: 2

Related Questions