Skip to content

Instantly share code, notes, and snippets.

@WillSullivan
Created January 22, 2019 16:54
Show Gist options
  • Save WillSullivan/6f550f45db2b3f0c7674804c7390052b to your computer and use it in GitHub Desktop.
Save WillSullivan/6f550f45db2b3f0c7674804c7390052b to your computer and use it in GitHub Desktop.
Can i catch an Op Cancelled Expression
class Program
{
static async Task Main(string[] args)
{
var cts = new CancellationTokenSource();
var task = DoSomeWork(cts.Token);
await Task.Delay(2000);
cts.Cancel();
await Task.Delay(2000);
WriteLine("Yep, we done");
ReadLine();
}
private async static Task DoSomeWork(CancellationToken token)
{
try
{
while(true)
{
await Task.Delay(1000);
token.ThrowIfCancellationRequested();
}
}
catch(OperationCanceledException)
{
WriteLine("Task cancelled!");
}
catch(Exception)
{
WriteLine("wth?");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment