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开发者_JAVA百科. Should I call Main()
from OnChange
, or is there another way that is better?
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.
Personally, this is what I think.
- Yes, you should call your
Main
from yourOnChange
method, if that is in fact what you want to do (i.e., just runMain
again). - But to prevent uncontrolled recursion, you should ensure that the file manipulation you're performing from within
Main
does not itself triggerOnChange
. This could be by either setting a flag or temporarily removing yourChanged
handler and adding it back when you're done.
Create one FileSystemWatcher object, which throws several events, you can handle every event differently.
精彩评论