CancellationTokenSource cts = new CancellationTokenSource();
private async void Button_Click_2(object sender, RoutedEventArgs e)
{
Task<string> t1 = Task.Run(StartFUUpdateCommandFun, cts.Token);
await Task.WhenAll(t1);
if (!t1.Result.Equals("Success!"))
{
throw new Exception(t1.Result);
}
}
private string StartFUUpdateCommandFun()
{
try
{
while (true)
{
cts.Token.ThrowIfCancellationRequested();
}
}
catch (OperationCanceledException)
{
cts = new CancellationTokenSource();
return "停止!";
}
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
cts.Cancel();
}