Async Concepts
Cancellation Signal
Use a cancellation flag to decide whether work should continue.
Cancellation Signal
CancellationSignal.cs
using System;
class Program
{
static void Main()
{
bool cancelRequested = ;
string nextStep = cancelRequested ? "stop" : "download";
string state = cancelRequested ? "cancelled" : "running";
Console.WriteLine($"cancelRequested={cancelRequested}");
Console.WriteLine($"nextStep={nextStep}");
Console.WriteLine($"state={state}");
}
}
using System;
class Program
{
static void Main()
{
bool cancelRequested = ;
string nextStep = cancelRequested ? "stop" : "download";
string state = cancelRequested ? "cancelled" : "running";
Console.WriteLine($"cancelRequested={cancelRequested}");
Console.WriteLine($"nextStep={nextStep}");
Console.WriteLine($"state={state}");
}
}
async
Long-running async work often receives a cancellation signal and checks it before starting the next step.