I have a console application that uses a fileSystemWatcher to monitor a mapped network path for new files. When the files appear, they get renamed based upon the content of the files. I want this to run 24/7. It works just fine for a time but I find that after a while the console "pauses" and seems to be waiting for input. If I hit return in the console, the application seems to catch up with all the events that seem to have been in "pending" awaiting some type of input. I can never seem to reproduce this issue to catch it in code where it is pausing or why.
I assumed this had something to do with the file system watcher losing its ability to monitor the network path due to outside factors (server restarting, network issues, etc.) so I recreate the file system watcher ever 30 minutes. I am pretty sure that actually has nothing to do with the situation now.
Essentially what I'm seeing is that the console is happily printing out the updates on the new files found, etc. but then it'll just stop (the program operation, not just the output) and will never continue, until I put focus onto the console app and hit some key and then all the pending events (like the timer elapses and the file system watcher re-creations) all happen.
The question is: Why is this happening, and how do I prevent it?
The nuts and bolts of the program looks like this:
static void Main(string[] args)
{
RefreshTimer = new Timer(1800000); //30 minutes
RefreshTimer.Elapsed += RefreshTimer_Elapsed;
RefreshTimer.Enabled = true;
writeHelp();
reCreateWatcher();
getInput();
}
private static void writeHelp()
{
Console.WriteLine("HELP:");
Console.WriteLine(@"");
Console.WriteLine("RENAMES FILES THAT ARE PLACED INTO THE WORKING FOLDER.");
Console.WriteLine("*************_Commands:_************");
Console.WriteLine("'runAll' = will run the program on all files curently existing in the folder. Note that if a file has already been renamed the program SHOULD attempt the rename, find the file has already been renamed, and move to the next file.");
Console.WriteLine("'quit' = exits program");
Console.WriteLine("'help' = shows this spiffy information.");
Console.WriteLine("[end]");
Console.WriteLine("");
}
private static void getInput()
{
Console.Write("Cmd ->");
string k = Console.ReadLine();
switch (k)
{
case "help":
writeHelp();
break;
case "runAll":
string[] allFiles = System.IO.Directory.GetFiles(PATH_TO_FILES);
foreach (string curr in allFiles)
{
parseTheFile(curr);
}
break;
case "quit":
return;
default:
Console.WriteLine("Huh? Unknown command!");
break;
}
getInput();
}
private static void reCreateWatcher()
{
Console.WriteLine("re-starting watcher...");
Console.WriteLine("Starting Monitor of: " + PATH_TO_FILES);
if (TheWatcher != null)
{
TheWatcher.Dispose();
}
deleteOldFiles();
try
{
TheWatcher = new FileSystemWatcher(PATH_TO_FILES, "*.pdf");
}
catch (Exception ex)
{
Console.WriteLine("AN ERROR OCCURED WHEN TRYING TO SET UP THE FILE SYSTEM WATCHER ON, '" + PATH_TO_FILES + "'. THE ERROR WAS: " + ex.Message);
Console.WriteLine("Hit any key to exit.");
Console.ReadKey();
Environment.Exit(0);
}
TheWatcher.NotifyFilter = NotifyFilters.LastWrite;
TheWatcher.Changed += theWatcher_Changed;
TheWatcher.EnableRaisingEvents = true;
}
static void theWatcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("New file found: " + e.Name);
parseTheFile(e.FullPath);
}