title: How can I alter XAML elements from a backgroundworker thread?
body: <p>In the following code example, I want to <strong>change the color</strong> of the Foreground text of a TextBox <strong>from my BackgroundThread</strong>, but it gets the <strong>error</strong>:</p>
  
  <blockquote>
    <p>This thread cannot access this object
    since it is in another thread.</p>
  </blockquote>
  
  <p><strong>What do I have to change in the code below so that the background worker thread can change the foreground color in the TextBox?</strong></p>
  
  <h1>Answer:</h1>
  
  <p>Thanks Andy, it was just that small oversight, here is the corrected code for posterity's sake:</p>
  
  <pre><code>using System.Windows;
  using System.ComponentModel;
  using System.Threading;
  using System.Windows.Media;
  
  namespace TestBackgroundWorker7338
  {
      public partial class Window1 : Window
      {
          private BackgroundWorker _worker;
          int _percentageFinished = 0;
  
          public Window1()
          {
              InitializeComponent();
              ButtonCancel.IsEnabled = false;
          }
  
          private void Button_Start(object sender, RoutedEventArgs e)
          {
              _worker = new BackgroundWorker();
              _worker.WorkerReportsProgress = true;
              _worker.WorkerSupportsCancellation = true;
  
              _worker.DoWork += (s, args) =&gt;
              {
                  BackgroundWorker worker = s as BackgroundWorker;
                  int numberOfTasks = 300;
                  for (int i = 1; i &lt;= numberOfTasks; i++)
                  {
                      if (worker.CancellationPending)
                      {
                          args.Cancel = true;
                          return;
                      }
  
                      Thread.Sleep(10);
                      float percentageDone = (i / (float)numberOfTasks) * 100f;
                      worker.ReportProgress((int)percentageDone);
                  }
              };
  
              _worker.ProgressChanged += (s,args) =&gt;
              {
                  _percentageFinished = args.ProgressPercentage;
                  ProgressBar.Value = _percentageFinished;
                  Message.Text = _percentageFinished + "% finished";
                  if (_percentageFinished &lt; 500)
                  {
                      Message.Text = "stopped at " + _percentageFinished + "%";
                  }
                  else
                  {
                      Message.Text = "finished";
                  }
  
                  if (_percentageFinished &gt;= 70)
                  {
                      InputBox.Foreground = new SolidColorBrush(Colors.Red);
                  }
                  else if (_percentageFinished &gt;= 40)
                  {
                      InputBox.Foreground = new SolidColorBrush(Colors.Orange);
                  }
                  else if (_percentageFinished &gt;= 10)
                  {
                      InputBox.Foreground = new SolidColorBrush(Colors.Brown);
                  }
                  else
                  {
                      InputBox.Foreground = new SolidColorBrush(Colors.Black);
                  }
  
              };
  
              _worker.RunWorkerCompleted += (s,args) =&gt;
              {
                  ButtonStart.IsEnabled = true;
                  ButtonCancel.IsEnabled = false;
                  ProgressBar.Value = 0;
              };
  
              _worker.RunWorkerAsync();
              ButtonStart.IsEnabled = false;
              ButtonCancel.IsEnabled = true;
  
          }
  
          private void Button_Cancel(object sender, RoutedEventArgs e)
          {
              _worker.CancelAsync();
          }
      }
  }
  </code></pre>