Created
January 22, 2019 16:54
-
-
Save WillSullivan/6f550f45db2b3f0c7674804c7390052b to your computer and use it in GitHub Desktop.
Can i catch an Op Cancelled Expression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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