title: WPF Problem with drawing Image multiple times using Thread and Dispatcher.Invoke
body: <p>I'm trying to draw stuff on Image control a bunch of times and I need it to be refreshed and viewable after each drawing, with user controls staying accessible, responsible and non-frozen.
  The drawing itself is pretty fast.</p>
  <p>I used Thread with Dispatcher.Invoke but it doesnt seem to work. MainWindow becomes irresponsive until cycle of drawing finishes.</p>
  <p>You may see that I tried 2 variants with cycle inside of Invoke and outside.
  Can someone give me advice on the problem, please?</p>
  <p>EDIT: view is an object that draws image on Bitmap and returns it as source for Image control.</p>
  <p>iView is that Image control on the MainWindow.</p>
  <pre><code>private void Draw()
  {
      // setting size of image
      view.DataHeight = 500;
      view.DataWidth = TDRConnection.MAX_DATAPOINTS;
      view.Height = (int)gView.ActualHeight;
      view.Width = (int)gView.ActualWidth;
  
      //drawing     
      iView.Source = view.DrawPixels(connection.GetMeasurementData(),
          TDRConnection.MAX_SUPPORTED_CHANNELS,
          portColors);
  }
  
  private void LiveDrawingAS()
  {
      Dispatcher.Invoke(() =&gt; {
          int safety = 0;
          for (; safety &lt; 300 &amp;&amp; doLiveDrawing; safety++) {
              Draw();
              //Thread.Sleep(100);
          }
          MessageBox.Show($&quot;Done {safety} drawings&quot;);
      });
  }
  private void LiveDrawingNAS()
  {
      int safety = 0;
      for (; safety &lt; 300 &amp;&amp; doLiveDrawing; safety++) {
          Dispatcher.Invoke(() =&gt; {
              Draw();
              //Thread.Sleep(100);                    
          });
      }
      MessageBox.Show($&quot;Done {safety} drawings&quot;);
  }
  
  private void bLive_Click(object sender, RoutedEventArgs e)
  {
      doLiveDrawing = true;
      new Thread(new ThreadStart(LiveDrawingAS)).Start();
  }
  
  private void bStop_Click(object sender, RoutedEventArgs e)
  {
      doLiveDrawing = false;
  }
  </code></pre>
  <p>EDIT2: I've put creating of bitmap outside of Invoke, and now MainWindow is responsive, but the Image control is empty, so its still no use:</p>
  <pre><code>    private void LiveDrawingNAS()
      {
          WriteableBitmap wbm;
  
          int safety = 0;
          for (; safety &lt; 30 &amp;&amp; doLiveDrawing; safety++) {
              wbm = DrawBitmap();
              Dispatcher.Invoke(() =&gt; {
                  iView.Source = wbm;
              });
          }
          MessageBox.Show($&quot;Done {safety} drawings&quot;);
      }
  </code></pre>
  <p>Okay, so I found the solution. It requires Freeze for bitmap:</p>
  <pre><code>    private void LiveDrawingOutUI()
      {
          WriteableBitmap wbm;
  
          int safety = 0;
          for (; safety &lt; 300 &amp;&amp; doLiveDrawing; safety++) {
              wbm = DrawBitmap();
  
              wbm.Freeze();
              Dispatcher.Invoke(() =&gt; {
                  iView.Source = wbm;
              });
          }
          MessageBox.Show($&quot;Done {safety} drawings&quot;);
      }
  </code></pre>
  <p>Cheers!</p>