title: Draw on a form by a separate thread
body: <p>I'm trying to build a multithreaded game where I have a separate thread for painting on the form which is not the main thread. this brings us to thread-safe technics which I've read many articls about, but I'm not really sure I got it correctly.</p>
  
  <p>my problem is that I have a structure where every data object is painting it self on the form so I didn't figure out how to implement it.</p>
  
  <p>this is a snippet of my working mono-thread code:</p>
  
  <pre><code>public partial class Form1 : Form
  {
      GameEngine Engine;
      public Form1()
      {
          InitializeComponent();
          Engine = new GameEngine();
      }
      protected override void OnPaint(PaintEventArgs e)
      {
          Engine.Draw(e.Graphics);
      }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>class GameEngine
  {
  
      Maze Map;
      List&lt;Player&gt; Players;
  
      public void Draw(Graphics graphics)
      {
              Map.Draw(graphics);
              foreach (var p in Players)
              {
                  p.Draw(graphics);
              }
      }
  </code></pre>
  
  <p>}</p>
  
  <p>so please can anyone give me a hint or a link to good article helping me to learn how to separate the drawing on an another thread?.</p>
  
  <p>[Edit]</p>
  
  <p>I managed to implement what I intended to do
  and this is how I coded it</p>
  
  <pre><code>    protected override void OnPaint(PaintEventArgs e)
      {
          formGraphics = e.Graphics;
          DisplayThread = new Thread(new ThreadStart(Draw));
          DisplayThread.Start();
      }
  
      private void Draw()
      {
          if (this.InvokeRequired)
          {
              this.Invoke(new DrawDelegate(this.Draw));
          }
          else
          {
              Engine.Draw(formGraphics);
          }
      }
  </code></pre>
  
  <p>but I got an ArgumentException : Parameter is not valid</p>
  
  <p>would you please point to the error in that code</p>