我有以下代码。但我有立即打破这个循环的问题。当我把if / break语句放到这个循环的底部时,它会在循环结束时中断循环。所以我在每个延迟语句之后放置if / break语句。虽然它有效(不能很好地工作),但我搜索另一个修复,在此循环的任何地方立即打破此循环
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;
}
}
}
答案 0 :(得分:0)
您需要做的是将btns
替换为CancellationToken
,这样可以让您提前停止Task.Delay
,但是当它停止时会抛出异常,所以我们需要稍微修改代码以捕获该异常。
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;
}
}
}