title: C# System.InvalidOperationException: Object is currently in use elsewhere
body: <p>I have already seen questions on the same topic but couldn't figure out how to solve this problem. In my game, I am using two threads one <code>Logic</code> thread and the <code>UI</code> thread. This is the problem I'm having</p>
  
  <pre><code>System.InvalidOperationException: Object is currently in use elsewhere.
     at System.Drawing.Graphics.FromImage(Image image)
     at GECS.Core.Game.UpdateLoop() in c:\Us....ore\Game.cs:line 82
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>
  
  <p>I was rendering only on the UI thread. Here's my rendering loop. I placed it right in the constructor.</p>
  
  <pre><code>while (true) {
      if (!Created) {
          break;
      }
      if (Win32.PeekMessage(out msg, IntPtr.Zero, 0, 0, (uint)Win32.PM.REMOVE)) {
          if (msg.message == (uint)Win32.WindowsMessage.WM_QUIT) {
              break;
          } else {
              Win32.TranslateMessage(ref msg);
              Win32.DispatchMessage(ref msg);
          }
      } else {
          Text = Global.TITLE;
          now = GetCurrentTime();
          Graphics front = Graphics.FromHwnd(Handle);
          front.DrawImage(backBuffer, 0, 0);
          front.Dispose();
      }
  }
  </code></pre>
  
  <p>And my logic loop</p>
  
  <pre><code>while (Created) {
      now = GetCurrentTime();
      while (now &gt; game_time) {
          elapsedTime = 1000/Global.ACTUAL_STEPS_FOR_SECOND;
          Update(elapsedTime);
          Map.UpdateObjects(elapsedTime);
          game_time += step_size;
      }
      Graphics g = Graphics.FromImage(backBuffer);   // This line is error
      g.Clear(Color.WhiteSmoke);
      Render(g);
      g.Dispose();
  }
  </code></pre>
  
  <p>How can I solve this problem? Already tried pasting the logic loop on top of render loop which made my game so slow.</p>
  
  <p>Thanks</p>