-3

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 immediately anywhere in this loop

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>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;
            }

        }
    }
Safar Safarli
  • 81
  • 2
  • 8
  • 3
    I beleive the question you are really trying to ask is "*How do I cause my code to break out early when `btns` is true while I am waiting for a `Task.Delay` to finish?*". To answer that we need to know what is `btns` and how is it set to `true`? There is a easy solution using a `CancellationToken` but we need to see how `btns` is set to `true` to show you a example of how to use it (It will replace `btns`) – Scott Chamberlain Aug 10 '16 at 21:59
  • i used " private bool btns = false; " statement and. I set it true with button click. – Safar Safarli Aug 10 '16 at 22:03
  • [Edit your question](http://stackoverflow.com/posts/38884208/edit) and inlcude the button click code so I can reference it in my answer. – Scott Chamberlain Aug 10 '16 at 22:03
  • yes. i want my loop doesn't wait execute Task.Delay to finish when i call break. – Safar Safarli Aug 10 '16 at 22:11
  • Don't put code in comments. You need to edit your question and put it there. – Scott Chamberlain Aug 10 '16 at 22:11

1 Answers1

0

What you need to do is replace btns with a CancellationToken, this will let you stop the Task.Delay early, however when it stops it a exception will be thrown so we need to modify the code slightly to catch that exception.

private CancellationTokenSource tokenSource = new CancellationTokenSource();

private void button19_Click(object sender, EventArgs e)
{
    //Signala the token source to cancel;
    tokenSource.Cancel();

    //Make a new token source, autoshot will still be listening to the old source because we passed in the token as an argument.
    tokenSource = new CancellationTokenSource();

    activatepuckbuttons();
    btn_status = true;
    label25.Visible = false;
    pictureBox1.Visible = false;
}

//We don't make this async void because we don't use await.
private void StartAutoShotButton_Click(object sender, EventArgs e)
{
    //We do not await this because we are using it in a fire-and-forget fashion.
    autoshot(tokenSource.Token);
}

//It is a bad habit to do `async void` when not using a event. Just have it return a task but not wait on it.
private async Task autoshot(CancellationToken token)
{
    try
    {
        while (true)
        {

            myport.Write("A1");
            label24.Text = "A";
            await Task.Delay(700, token);

            myport.Write("A0");
            label24.Text = "";
            await Task.Delay(2300, token);


            // ---


            myport.Write("B1");
            label24.Text = "B";
            await Task.Delay(700, token);

            myport.Write("B0");
            label24.Text = "";
            await Task.Delay(2300, token);
        }

        //...

    }
    catch(OperationCanceledException ex)
    {
        if(ex.CancellationToken 1= token)
        {
            //Something other than our token caused a cancel, we should let the error bubble up.
            throw;
        }
    }
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431