Implementing a delayed execution task with cancellation
I have a situation where I want to allow a user to type into a WPF TextBox, on each keystroke I call OnPropertyChanged and push to my binding source (using SourceUpdatedTrigger=PropertyChanged) I want there to be a short delay before attempting to process the data (say... 1 second) Think of this like a spell check system where it waits for the user to stop typing before telling them they can't spell.
My issue is:
await TaskEx.Delay(1000, cts.Token); await TaskEx.RunEx(PollAsync, cts.Token);
works fine until you actually cancel, but then the cancelation token source is permanantly in the "cancelation requested" state, and I have to reinstanciate the CTS... this seems... fundamentally wrong... Is there a better way to "reset" the CTS, yet still notify existing tokens that they should cancel?
No, a CancellationTokenSource can only be cancelled once - as you say, you need to create a new one after you've cancelled it, for further tasks.