title: How to update a WPF Control from the TPL Task?
body: <p>How to update a WPF Control from the TPL Task? </p>
  
  <p>Fine so I tried some scenarios to use Dispatcher but anyway it gives the error. I need help guys!</p>
  
  <pre><code>private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          //Application.Current.Dispatcher.Invoke((Action)MyInit); 
          backgroundDBTask = Task.Factory.StartNew(() =&gt;
              {
                  DoSomething1();
              }, TaskCreationOptions.LongRunning);
          backgroundDBTask.ContinueWith((t) =&gt;
          {
              // ... UI update work here ...
          },             
          TaskScheduler.FromCurrentSynchronizationContext());             
      }
  
      void DoSomething1()
      {
          // MyInit();
          int number = 0;
          while (true)
          {
              if (state)
              {
                  Thread.Sleep(1000);
                  Console.WriteLine("Begin second task... {0}", number++);
                 // mostCommonWords = GetMostCommonWords(words) + string.Format("   Begin second task... {0}", number++);
  
                  textBox2.Text = (number++).ToString(); // Gives the error
  
                  Dispatcher.BeginInvoke(); // How it should be ?
              }
          }
      }
  </code></pre>
  
  <p>Thank you!</p>