Created
June 6, 2017 09:33
-
-
Save melvinlee/63bd43a18c07466d4213acfa3cbfab0b to your computer and use it in GitHub Desktop.
Long running task with cancellation tocken
This file contains hidden or 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
var cts = new CancellationTokenSource(); | |
var token = cts.Token; | |
Task.Factory.StartNew(() => | |
{ | |
while (true) | |
{ | |
if (cts.IsCancellationRequested) | |
{ | |
return; | |
} | |
token.WaitHandle.WaitOne(TimeSpan.FromSeconds(10)); //wait | |
} | |
}, token, TaskCreationOptions.LongRunning, TaskScheduler.Default); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment