title: Window frozen on drawing shape
body: <p>I have a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.aspx" rel="nofollow">Canvas</a> inside my WPF Window, and I want to draw a huge number of shapes (about hundred thousand shapes or more). Something like this:</p>
  
  <pre><code>&lt;Window&gt;
  &lt;Canvas&gt;
      &lt;!--&lt;Shape1/&gt;
      &lt;Shape2/&gt;
      ... and so on ... --&gt;
  &lt;/Canvas&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>To draw shapes i have a method that does a job like this:</p>
  
  <pre><code>public void DrawShape()
  {
      // Create the shape
      ShapeT MyShape = new ShapeT();
      // Set shape's properties (like Width, Fill, ...)
  
      // Add shape as MyCanvas children.
      MyCanvas.Children.Add(MyShape);
  }
  </code></pre>
  
  <p>Then to draw, for example, forty thousand shapes I do:</p>
  
  <pre><code>for (uint i = 0; i &lt; 0x9C40; ++i)
       DrawShape();
  </code></pre>
  
  <p><strong>The problems are:</strong></p>
  
  <ol>
  <li><p>Until all the figures are not drawn, i.e until the loop is completed, no figure is drawn and my window not responding for all time.</p></li>
  <li><p>Even after the loop has been completed, it still takes several seconds before the figures are drawn on the video, and my window continues to be frozen.</p></li>
  </ol>
  
  <p>I tried to use <a href="http://msdn.microsoft.com/en-us/library/System.Windows.Threading.Dispatcher.aspx" rel="nofollow">Dispatcher</a>:</p>
  
  <pre><code>Dispatcher.BeginInvoke(DispatcherPriority.Background, new MyDrawDelegate(DrawShape));
  </code></pre>
  
  <p>I tried to create a new thread:</p>
  
  <pre><code>Thread DrawThread = new Thread(new ThreadStart(DrawShape));
  DrawThread.SetApartmentState(ApartmentState.STA);
  DrawThread.Priority = ThreadPriority.Lowest;
  DrawThread.Start();
  </code></pre>
  
  <p>but there were no changes.
  How can I fix these problems?</p>
  
  <p>PS: for performance reasons I draw my shapes using <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.visual.aspx" rel="nofollow">Visual</a> and not <a href="http://msdn.microsoft.com/en-us/library/system.windows.shapes.shape.aspx" rel="nofollow">Shape</a> objects.</p>