title: BackgroundWorker questions
body: <p>I have UI WPF application.</p>
  
  <p>May by some one have any ideas why this code is not working?</p>
  
  <p>Case 1:</p>
  
  <pre><code>BackgroundWorker worker = new BackgroundWorker();
  worker.DoWork += delegate
  {
     //some logic
  };
  
  worker.RunWorkerAsync();
  </code></pre>
  
  <p>In that case i am getting exception 
  The calling thread cannot access this object because a different thread owns it. 
  Then i changed it to this: </p>
  
  <pre><code>BackgroundWorker worker = new BackgroundWorker();
  worker.DoWork += delegate 
  { 
    this.Dispatcher.BeginInvoke(
      new Action(() =&gt;  { //my code here }), null); 
  }; 
  </code></pre>
  
  <p>After that UI getting frozen during executing this code. Like it is executing in the same thread</p>
  
  <p>Case 2:</p>
  
  <pre><code>BackgroundWorker worker = new BackgroundWorker();
  worker.RunWorkerAsync(new Action(() =&gt;
  {
   //some code here
  }));
  </code></pre>
  
  <p>In that case code inside Action not executed.</p>
  
  <p>//----------------------------------UPDATED--------------------------------------//<br>
  Thank you guy's for help
  The reason why my code didn't work properly is mentioned below. I did access some UI elements in background thread. Now I am getting all the values from UI elements before the call to BackgroundWorker. I now declare new variables, assign them all values from required UI elements, and then I am passing these variables to BackgroundWorker instead of the UI elements (which is what it originally did).  </p>