Problems with Cancellation
-
21. února 2012 19:29
Hello,
I have taking some issues with CancellationToken.
For example: In a form I've got 2 buttons: One of them executes and async process, that takes a CancellationToken, the other one makes a Cancel to CancellationTokenSource.
Well, when I try to review the task status is not Cancelled. In the task Action I have tried with token.IsCancellationRequested and token.ThrowIfCancellationRequest... Catching the OperationCancelledException... but same problem...
How can I take that result succesfully?
Regards
Javier Torrecilla
Para el correcto funcionamiento, y que otros usuarios se puedan beneficiar de la solucion de esta pregunta por favor marca las respuestas que te hayan ayudado como "Respuesta".
Si la respuesta te ha sido util Votala.
Mi Blog: Jtorrecilla
Enlace a Faq de Winforms en Ingles Muy bueno
TabControl con Mejoras
Všechny reakce
-
22. února 2012 4:23
It's confusing at first... the example for the cancelationtokensource shows this code:
var tokenSource2 = new CancellationTokenSource();
CancellationToken ct = tokenSource2.Token;Notice that creations of the tokenSource2 by default contains a Token. The var CT then is the token used within the task to determine cancellation as shown here:
var task = Task.Factory.StartNew(() =>
{
// Were we already canceled?
ct.ThrowIfCancellationRequested();
bool moreToDo = true;
while (moreToDo)
{
// Poll on this property if you have to do
// other cleanup before throwing.
if (ct.IsCancellationRequested)
{
// Clean up here, then...
ct.ThrowIfCancellationRequested();
}
}
}, tokenSource2.Token); // Pass same token to StartNew.But who does the cancellation? Someone that issues this command does it:
tokenSource2.Cancel();
And that statement is found outside of the task construct...
JP Cowboy Coders Unite!
-
22. února 2012 5:22
Hi again.
If you run this sample:
private async void button1_Click(object sender, EventArgs e) { CancellationToken ct = tokenSource2.Token; Task task = null; try { task = Task.Factory.StartNew(() => { // Were we already canceled? ct.ThrowIfCancellationRequested(); try { bool moreToDo = true; while (moreToDo) { // Poll on this property if you have to do // other cleanup before throwing. if (ct.IsCancellationRequested) { // Clean up here, then... ct.ThrowIfCancellationRequested(); } } } catch (OperationCanceledException) { } }, tokenSource2.Token); } catch (Exception) { throw; } await task; var t = task.Status; } private void button2_Click(object sender, EventArgs e) { tokenSource2.Cancel(); } }
t is RanToCompletionJavier Torrecilla
Para el correcto funcionamiento, y que otros usuarios se puedan beneficiar de la solucion de esta pregunta por favor marca las respuestas que te hayan ayudado como "Respuesta".
Si la respuesta te ha sido util Votala.
Mi Blog: Jtorrecilla
Enlace a Faq de Winforms en Ingles Muy bueno
TabControl con Mejoras -
22. února 2012 13:29
That's because you're catching the OperationCanceledException inside the task. This is telling it to complete normally even though it was cancelled.
Try this instead:
private async void button1_Click(object sender, EventArgs e) { CancellationToken ct = tokenSource2.Token; Task task = null; try { task = Task.Factory.StartNew(() => { ct.ThrowIfCancellationRequested(); bool moreToDo = true; while (moreToDo) { if (ct.IsCancellationRequested) { ct.ThrowIfCancellationRequested(); } } }, ct); await task; } catch (OperationCanceledException) { } MessageBox.Show(task.Status.ToString()); }
-Steve
Programming blog: http://nitoprograms.blogspot.com/
Including my TCP/IP .NET Sockets FAQ
and How to Implement IDisposable and Finalizers: 3 Easy Rules
Microsoft Certified Professional Developer
How to get to Heaven according to the Bible- Označen jako odpověď jtorrecillaMVP 23. února 2012 9:08
-
22. února 2012 15:30
Later I will check. Hope It Works!
Thanks!
Javier Torrecilla
Para el correcto funcionamiento, y que otros usuarios se puedan beneficiar de la solucion de esta pregunta por favor marca las respuestas que te hayan ayudado como "Respuesta".
Si la respuesta te ha sido util Votala.
Mi Blog: Jtorrecilla
Enlace a Faq de Winforms en Ingles Muy bueno
TabControl con Mejoras -
22. února 2012 17:07
Hi again Stepehn,
I have tested but with no sucess.
I set the same code and I've got an OperationCancelledException at runtime "not catched" :(
Any other workAround?
Javier Torrecilla
Para el correcto funcionamiento, y que otros usuarios se puedan beneficiar de la solucion de esta pregunta por favor marca las respuestas que te hayan ayudado como "Respuesta".
Si la respuesta te ha sido util Votala.
Mi Blog: Jtorrecilla
Enlace a Faq de Winforms en Ingles Muy bueno
TabControl con Mejoras -
22. února 2012 23:18
The debugger is intercepting it.
Just hit Go again (or run it outside the debugger).
-Steve
Programming blog: http://nitoprograms.blogspot.com/
Including my TCP/IP .NET Sockets FAQ
and How to Implement IDisposable and Finalizers: 3 Easy Rules
Microsoft Certified Professional Developer
How to get to Heaven according to the Bible- Označen jako odpověď jtorrecillaMVP 23. února 2012 9:08
-
22. února 2012 23:37
-
23. února 2012 6:43
@Stephen that works, but its a bit of smell :(
@Paulo I have using Developer Preview.
Javier Torrecilla
Para el correcto funcionamiento, y que otros usuarios se puedan beneficiar de la solucion de esta pregunta por favor marca las respuestas que te hayan ayudado como "Respuesta".
Si la respuesta te ha sido util Votala.
Mi Blog: Jtorrecilla
Enlace a Faq de Winforms en Ingles Muy bueno
TabControl con Mejoras

