I have an app which is checking a txt file for new entries (lines) to process them and output the result into a richtextbox. After changing the file-checking from a simple loop to a FileSystemWatcher, it stops mid execution without any reason.
I have tried setting up breakpoints and go through the code step by step, set up try-catch blocks with no luck.
FileSystemWatcher setup:
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
fileSystemWatcher.Path = (logfile_path.Substring(0, logfile_path.Length-14));
fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
fileSystemWatcher.Filter = "iCCard_Log.log";
fileSystemWatcher.Changed += new FileSystemEventHandler(FileSystemWatcher_Changed);
fileSystemWatcher.EnableRaisingEvents = true;
private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
Checklog(); //calls a long part, which ends in Checkmain()
}
Problem presents at this line:
private void Checkmain()
{
RichTextBoxExtensions.AppendText_Error(errorBox, Environment.NewLine + "INFO: " + name + " - " + logline + " - " + firstclassstart_time.ToString(), Color.Red);
....
}
AppendText_Error:
public static class RichTextBoxExtensions
{
public static void AppendText_Error(this RichTextBox box, string text, Color color)
{
box.SelectionStart = box.TextLength; //stops here
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
}
There is also this:
private void errorBox_TextChanged(object sender, EventArgs e)
{
errorBox.SelectionStart = errorBox.Text.Length;
errorBox.ScrollToCaret();
}
The code stops at box.SelectionStart = box.TextLength;
If I change back to the loop method which is calling the exact same things, it works perfectly well (writes the result line into the richtextbox and also writes it out into a txt). If the startpoint is the filesystemwatcher it will just stop at the mentioned line without doing anything.
When the program starts up it is running the whole process once to check if some parsers are working and it does not stop and correctly gives the expected result, even if it's called by the filesystemwatcher at the start. If it's triggered again for actual processing it won't work for the second time.