title: C# InvalidOperationException when threading
body: <p>I'm trying to make a function run every 200 milliseconds so that it can show the time difference between when the program first started and right now. I tried threading with this code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Timers;
  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 ComputerTimer
  {
      public partial class MainWindow : Window
      {
          private DateTime startTime, endTime;
          private static System.Timers.Timer timer1;
          private bool running = true;
  
          public MainWindow()
          {
              InitializeComponent();
  
              startTime = DateTime.Now;
  
              //makes new timer with 200 milliseconds interval
              timer1 = new System.Timers.Timer(200);
              timer1.Elapsed += new ElapsedEventHandler(timer1_Tick);
              timer1.Interval = 200;
              timer1.Enabled = true;
  
          }
  
          private void timer1_Tick(object sender, EventArgs e)
          {
              while (running)
              {
                  endTime = DateTime.Now;
                  TimeSpan span = endTime - startTime; //gets difference between now and when the program was started
                  Title.Content = span.ToString().Substring(0, 8); //gets first 8 characters (taking out milliseconds)
              }
          }
  
          private void btnStop_Click(object sender, RoutedEventArgs e)
          {
              //when button is pressed to stop timer
              running = false;
          }
      }
  }
  </code></pre>
  
  <p>But this just throws the exception 'InvalidOperationException' and says "Additional information: The calling thread cannot access this object because a different thread owns it." about line 48</p>
  
  <pre><code>Title.Content = span.ToString().Substring(0, 8); //gets first 8 characters (taking out milliseconds)
  </code></pre>
  
  <p>I'm quite confused what to do from here and have searched all over stack overflow looking for an answer but nothing seems to work. I have also tried DispatcherTimer but with no luck.  </p>
  
  <p><strong>Edit: This is the answer which worked for me for anyone looking over this in the future</strong></p>
  
  <pre><code>namespace ComputerTimer
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          private DateTime startTime, endTime;
          private DispatcherTimer dtClockTime;
  
          public MainWindow()
          {
              InitializeComponent();
  
              startTime = DateTime.Now;
  
              dtClockTime = new DispatcherTimer();
  
              dtClockTime.Interval = new TimeSpan(0, 0, 0, 0, 200); //in days, Hour, Minutes, Seconds, millis
              dtClockTime.Tick += dtClockTime_Tick;
  
              dtClockTime.Start();
  
          }
  
          private void dtClockTime_Tick(object sender, EventArgs e)
          {
              endTime = DateTime.Now;
              TimeSpan span = endTime - startTime; //gets difference between now and when the program was started
              Title.Content = span.ToString().Substring(0, 8);
          }
  
          private void btnStop_Click(object sender, RoutedEventArgs e)
          {
              //when button is pressed to stop timer
              dtClockTime.Stop();
          }
      }
  }
  </code></pre>