title: calling draw rect does nothing sometimes
body: <p>i have a c# form which i want to use to draw a box moving continuously across the screen</p>
  
  <p>to achieve this moving box i have the following function.</p>
  
  <pre><code>    public void draw_forever() {
          int counter = 0;
          while (1 &lt; 2) {
              counter += 1;
              Debug.WriteLine("yo");
              draw_rect(this, 0, 0, this.Width, this.Height, 1); // create a white box to clear the screen 
              draw_rect(this, counter, 150, 20, 20,0); // create a black box that moves accross the screen 
              if (counter &gt; this.Width) { counter = 0; } // reset the box 
          }
      }
  </code></pre>
  
  <p>it does print "yo", but makes no changes to the form</p>
  
  <p>here i initialize my form,  call my function and draw 1 box to the screen.</p>
  
  <pre><code>    public Form1()
      {
          InitializeComponent();
          draw_rect(this, 50, 150, 20, 20, 0); // draw a first black box
          this.Shown += (s, e) =&gt;
          {
              Task.Run(() =&gt;
              {
                  draw_forever();
              }
          );};}
  </code></pre>
  
  <p>that 1 box i draw is all that you ever see. so we can see it never clears the screen with a white box</p>
  
  <p><a href="https://i.stack.imgur.com/DE5Kn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DE5Kn.png" alt="image"></a></p>
  
  <p>copy and paste the code below into a blank c# form to quickly copy what i have done </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Diagnostics;
  using System.Windows.Forms;
  
  namespace draw_forev
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
              draw_rect(this, 50, 150, 20, 20, 0); // draw a first black box
              this.Shown += (s, e) =&gt;
              {
                  Task.Run(() =&gt;
                  {
                      draw_forever();
                  }
              );
              };
          }
          public static void draw_rect(Form form, int x, int y, int width, int height, int colournum) // this function draws a block
          {
              Color thecolour;
              if (colournum == 0) { thecolour = Color.Black; }
              if (colournum == 1) { thecolour = Color.White; }
              form.Paint += OnPaint;
              void OnPaint(object sender, PaintEventArgs e)
              {
                  if (colournum == 0)
                  {
                      using (var Pen1 = new Pen(Color.Black, 9))
                      {
                          e.Graphics.DrawRectangle(Pen1, x, y, width, height);
                      }
                  }
                  if (colournum == 1)
                  {
                      using (var Pen1 = new Pen(Color.White, 9))
                      {
                          Rectangle rect = new Rectangle(x, y, width, height);
                          e.Graphics.DrawRectangle(Pen1, rect);
                      }
                  }
              }
          }
  
          public void draw_forever() {
              int counter = 0;
              while (1 &lt; 2) {
                  counter += 1;
                  Debug.WriteLine("yo");
                  draw_rect(this, 0, 0, this.Width, this.Height, 1); // create a white box to clear the screen 
                  draw_rect(this, counter, 150, 20, 20,0); // create a black box that moves accross the screen 
                  if (counter &gt; this.Width) { counter = 0; } // reset the box 
              }
          }
  
  
      }
  }
  </code></pre>