KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I want to schedule a task to start in x ms and be able to cancel it before it starts (or just at the beginning of the task). The first attempt would be something like var _cancelationTokenSource = new CancellationTokenSource(); var token = _cancelationTokenSource.Token; Task.Factory.StartNew(() => { token.ThrowIfCancellationRequested(); Thread.Sleep(100); token.ThrowIfCancellationRequested(); }).ContinueWith(t => { token.ThrowIfCancellationRequested(); DoWork(); token.ThrowIfCancellationRequested(); }, token); But I feel like there should be a better way, as this would use up a thread while in the sleep, during which it could be canceled. What are my other options?
Tags (comma-separated)
Save Edits
Cancel