title: Breaking a while loop consisting async await in C#
body: <p>I have following code. But i have problem with breaking this loop immediately. When i put if/break statement to the bottom of this loop, it breaks loop at the end of loop. So i put if/break statements after every delay statement. Although it worked(not worked "very well") but i search another fix to break this loop <strong>immediately anywhere in this loop</strong></p>
  
  <pre><code>private bool btns = false;
  
          private void button19_Click(object sender, EventArgs e)
      {
          btns = true;
          activatepuckbuttons();
          btn_status = true;
          label25.Visible = false;
          pictureBox1.Visible = false;
      }
  
  private async void autoshot()
      {
          while (1&gt;0)
          {
  
              myport.Write("A1");
              label24.Text = "A";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("A0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
  
  
              // ---
  
  
              myport.Write("B1");
              label24.Text = "B";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("B0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
  
  
              // ---
  
  
              myport.Write("C1");
              label24.Text = "C";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("C0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
  
  
              // ---
  
  
              myport.Write("D1");
              label24.Text = "D";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("D0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
  
  
              // ---
  
              myport.Write("E1");
              label24.Text = "E";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("E0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
              // ---
  
              myport.Write("F1");
              label24.Text = "F";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("F0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
  
              // ---
  
              myport.Write("G1");
              label24.Text = "G";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("G0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
  
              // ---
  
              myport.Write("H1");
              label24.Text = "H";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("H0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
  
              // ---
  
              myport.Write("I1");
              label24.Text = "I";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("I0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
  
              // ---
  
              myport.Write("J1");
              label24.Text = "J";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("J0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
  
              // ---
  
              myport.Write("K1");
              label24.Text = "K";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("K0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
  
              // ---
  
              myport.Write("L1");
              label24.Text = "L";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("L0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
  
              // ---
  
              myport.Write("M1");
              label24.Text = "M";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("M0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
  
              // ---
  
              myport.Write("N1");
              label24.Text = "N";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("N0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
  
              // ---
  
              myport.Write("O1");
              label24.Text = "O";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("O0");
              label24.Text = "";
              await Task.Delay(2300);
              if (btns == true)
              {
                  break;
              }
  
              // ---
  
              myport.Write("P1");
              label24.Text = "P";
              await Task.Delay(700);
              if (btns == true)
              {
                  break;
              }
              myport.Write("P0");
              label24.Text = "";
              await Task.Delay(2300);
  
              if (btns == true)
              {
                  break;
              }
  
          }
      }
  </code></pre>