none
Do I have to throw an exception for cancellation to work? RRS feed

  • Question

  • I see alot of references that call ThrowIfCancellationRequested() I want to start a bunch of threads to simulate a bunch of users an cancel all of them but I would rather not handle all of the exceptions. Can I cancel the Tasks in one operation by calling source.Cancel() and not have a bunch of unhandeled exceptions. Or, should I "observe" all of the Tasks and put a try/catch around the "observation"?

    Kevin Burton

    Tuesday, January 22, 2013 5:10 PM

Answers

All replies

  • I see alot of references that call ThrowIfCancellationRequested() I want to start a bunch of threads to simulate a bunch of users an cancel all of them but I would rather not handle all of the exceptions. Can I cancel the Tasks in one operation by calling source.Cancel() and not have a bunch of unhandeled exceptions. Or, should I "observe" all of the Tasks and put a try/catch around the "observation"?

    Kevin Burton

    The Cancellation mechanism in .NET 4 is cooperative.  This means that, when you call source.Cancel(), you're requesting that the individual tasks cancel, but it will not force them to cancel.  The token.ThrowIfCancellationRequested() method is a way for the tasks themselves to check to see if they should cancel, and if so, exit.

    If you're building the tasks and waiting on them, a single Task.WaitAll() will catch the cancelation and wrap them all up for you - so you really only need one exception handler at that point.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, January 22, 2013 5:22 PM
    Moderator
  • I should also add - you don't have to use ThrowIfCancellationRequested() - you can also just check CancellationToken.IsCancellationRequested within your Task, and exit gracefully (without exceptions).


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by KevinBurton Tuesday, January 22, 2013 6:12 PM
    Tuesday, January 22, 2013 5:51 PM
    Moderator
  • When I run under Visual Studio I get the message: "One or more errors occurred. A task was canceled." in the output window. Can I suppress this? Where in the process does this come from?


    Kevin Burton

    Tuesday, January 22, 2013 7:00 PM
  • When I run under Visual Studio I get the message: "One or more errors occurred. A task was canceled." in the output window. Can I suppress this? Where in the process does this come from?


    Kevin Burton

    You can subscribe to TaskScheduler.UnobservedTaskException (http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.unobservedtaskexception.aspx) to suppress the messages - in .NET 4, these would tear down your application, but in 4.5 they are just "swallowed".

    In general, if you raise a cancellation exception (or any exception) in a Task, you typically need to handle it.  I recommend reading Exception Handling in the TPL: http://msdn.microsoft.com/en-us/library/dd997415.aspx


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, January 22, 2013 7:09 PM
    Moderator