I have been playing round with the async ctp
this morning and have a simple program with a button
and a label
. Click the button
and it starts updating the label
, stop the button
it stops writing to the label
. However, I'm not sure how to reset the CancellationTokenSource
so that I can restart the process. My code is below:
public partial class MainWindow : Window
{
CancellationTokenSource cts = new CancellationTokenSource();
public MainWindow()
{
InitializeComponent();
button.Content = "Start";
}
async Task DoWork(CancellationToken cancelToken)
{
int i = 0;
while (!cancelToken.IsCancellationRequested)
{
label.Content = i++.ToString();
await TaskEx.Delay(50, cancelToken);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (button.Content == "Start")
{
button.Content = "Stop";
DoWork(cts.Token);
}
else
{
button.Content = "Start";
cts.Cancel();
}
}
}
Even I had same problem and I figured it out that, best way to solve it is to create cancellation token source newly just before calling the method.
this is what I do on my start button click:
I change the caption for the same button to cancel and when a click occurs on cancel, i call
Here is the full code:
You need to recreate the
CancellationTokenSource
- there is no way to "reset" this once you set it.This could be as simple as: