I have a running tool that writes me the status in a txt file.
Now I'm building an other tool that observe this status and display it to me. So I observe the txt file if there are changes and if there are changes, I read from the txt file and so on. But I got this error on box1_wnr.Content = WNR_KNR[0];:
System.InvalidOperationException: 'The calling thread cannot access this object because the object is owned by another thread.'
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace BDE_Leitstand
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string[] line;
        string[] lines;
        string[] WNR_KNR;
        char separator = ',';
        char separator2 = ';';
        public MainWindow()
        {
            InitializeComponent();
            
        }
        // Bei start alle zustände abfragen
        // evtl über Knopf oder autostart
        // daraus observer starten
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var watcher = new FileSystemWatcher(@"C:/UserData/z0047shw/OneDrive - Siemens Energy/Documents/KHT/Montagelinie_Projekt/BDE/DB");
            watcher.NotifyFilter = NotifyFilters.Attributes
                                 | NotifyFilters.CreationTime
                                 | NotifyFilters.DirectoryName
                                 | NotifyFilters.FileName
                                 | NotifyFilters.LastAccess
                                 | NotifyFilters.LastWrite
                                 | NotifyFilters.Security
                                 | NotifyFilters.Size;
            watcher.Changed += OnChanged;
            watcher.Filter = "Box1_Status.txt";
            watcher.IncludeSubdirectories = true;
            watcher.EnableRaisingEvents = true;
        }
        private void OnChanged(object sender, FileSystemEventArgs e)
        {
            lines = System.IO.File.ReadAllLines(@"C:/UserData/z0047shw/OneDrive - Siemens Energy/Documents/KHT/Montagelinie_Projekt/BDE/DB/Box1_Status.txt");
            if (lines[0]==null)
            {
                // nächstes aufmachen
            }
            else
            {
                line = lines[0].Split(separator);
                WNR_KNR = line[0].Split(separator2);
                box1_wnr.Content = WNR_KNR[0];
                box1_knr.Content = WNR_KNR[1];
                if (line[1] == " working")
                {
                    rec_box1.Fill = new SolidColorBrush(Color.FromRgb(0, 238, 0));
                }
                else if (line[1] == " absence")
                {
                    rec_box1.Fill = new SolidColorBrush(Color.FromRgb(255, 255, 0));
                }
                else if (line[1] == " disturbance")
                {
                    rec_box1.Fill = new SolidColorBrush(Color.FromRgb(255, 0, 0));
                }
            }            
        }
    }
}
