0

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.

Xerren
  • 11
  • 1
  • 1
  • The method called is running on a different thread than the "UI thread". Look up `Control.Invoke` and `Control.InvokeRequired`, and query about "update" and "UI Thread" and you should find something – Flydog57 Jan 25 '19 at 22:00
  • See [get filesystemwatcher events to occur on main UI thread](https://stackoverflow.com/a/22491246/719186) – LarsTech Jan 25 '19 at 22:04
  • [FileSystemWatcher.SynchronizingObject](https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher.synchronizingobject?view=netframework-4.7.2) – Jimi Jan 25 '19 at 22:09
  • Thank you @Flydog57 for pointing me into the right direction! I didn't know they run on different threads. Also thanks LarsTech for the quick example. Solved the issue. – Xerren Jan 26 '19 at 09:46

0 Answers0