开发者

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.

  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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜