I have a program that calls a Filewatcher function, like this
Main()
{
watch();
console.Readkey();
}
I have the console.readkey at the end because I don’t want the console to display ‘Press any key to continue’ while the file is being watched for changes. Now, if a change is detected, another function gets triggered off, which does its thing, and eventually calls main again, which once again stops at console.readkey.
At this point, I am experiencing some weird problems like the program requires two key inputs to continue. I’m guessing thats because the console.readkey from before is still waiting for an input, so the first input goes there, and then the second input goes to the second console.readkey.
So, my question is, the first time when the filewatcher gets triggered, can i, via code, feed something to the console.readkey, thats waiting for a user input?
Console.ReadKeywill block the program until you press a key (at which point it will read that and return).It sounds like you, in this situation, need to change your logic to just loop indefinitely (or until some other condition is reached). Instead of using
Console.ReadKeyto prevent the application from ending, you should consider re-writing it like:This will make the program run “forever”, until you set
exitProgramto true, at which point it will exit normally. The “watch” will not get called continually, sinceresetEventwill block indefinitely. When your “work” finishes (after the FileSystemWatcher event handler completes), callresetEvent.Set(). This will cause the loop to repeat one more time, re-triggering your watch code.It works by using an AutoResetEvent to prevent the watcher from “rewatching” the same file repeatedly.