Skip to content

Instantly share code, notes, and snippets.

@melvinlee
Created June 6, 2017 09:33
Show Gist options
  • Save melvinlee/63bd43a18c07466d4213acfa3cbfab0b to your computer and use it in GitHub Desktop.
Save melvinlee/63bd43a18c07466d4213acfa3cbfab0b to your computer and use it in GitHub Desktop.
Long running task with cancellation tocken
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