title: Responsive Form without await
body: <p>Suppose you are doing a lot of stuff inside a form.</p>
  
  <pre><code>while(true)
  {
      DoStuff();
  }
  </code></pre>
  
  <p>This results in problems: while the loop is executing the form stops redrawing and locks up.</p>
  
  <p>The goto solution is to use async/await.</p>
  
  <pre><code>while(true)
  {
      await Task.Run(DoStuff);
  }
  </code></pre>
  
  <p>...or some variation on that.</p>
  
  <p>However if the cycle period is small this will result in new task objects popping up and GC struggling to  disintegrate them. Though a modern machine can handle this without a problem, I think it is still desirable to avoid that.</p>
  
  <p>I have found that you can solve the redraw problem with <code>Control.Invalidate()</code>. It will redraw the form, as long as it stays in focus.</p>
  
  <pre><code>while(true)
  {
      DoStuff();
      MyControl.Invalidate();
      //or
      MyForm.Refresh();
  }
  </code></pre>
  
  <p>However I have no idea how to avoid the locking.</p>
  
  <p>As far as I know forms run on one thread, so while my code is executing the form is unable to process windows messages. In a cpp program you would just go through the message buffer every cycle yourself. But what can you do here?</p>