Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8888305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:59:51+00:00 2026-06-14T21:59:51+00:00

When running in a Console application under Visual Studio 2012 Listing_07 of Adam Freeman’s

  • 0

When running in a Console application under Visual Studio 2012 Listing_07 of Adam Freeman’s Pro .NET 4 Parallel Programming in C#, I get an exception when I try to cancel the task. To me there seems to be no big mistery as to the why of this, as we’re throwing (or so it seems) a OperationCanceledException when a cancellation attempt is detected in the task.

Is this a bug or am I missing something? The problem is arising in basically all his task cancellation examples! I can only assume something in the Task Library was changed since the book came to the stores (2010) ?

// create the cancellation token source
CancellationTokenSource tokenSource
    = new CancellationTokenSource();

// create the cancellation token
CancellationToken token = tokenSource.Token;

// create the task
Task task = new Task(() => {
    for (int i = 0; i < int.MaxValue; i++) {
        if (token.IsCancellationRequested) {
            Console.WriteLine("Task cancel detected");
            throw new OperationCanceledException(token);
        } else {
            Console.WriteLine("Int value {0}", i);
        }
    }
}, token);

// wait for input before we start the task
Console.WriteLine("Press enter to start task");
Console.WriteLine("Press enter again to cancel task");
Console.ReadLine();

// start the task
task.Start();

// read a line from the console.
Console.ReadLine();

// cancel the task
Console.WriteLine("Cancelling task");
tokenSource.Cancel();

// wait for input before exiting
Console.WriteLine("Main method complete. Press enter to finish.");
Console.ReadLine();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:59:52+00:00Added an answer on June 14, 2026 at 9:59 pm

    The exception is caught by the TPL, and the Task is put into the Canceled state. So, while you will see the exception (if you have exceptions turned on in the debugger), it will be handled.

    As leon mentioned, you should be using the CancellationToken.ThrowIfCancellationRequested method, rather than explicitly doing the check + throw yourself.

    This page gives good details on cancellation of Tasks:
    http://msdn.microsoft.com/en-us/library/dd997396.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider one console application is created and running under visual studio 2005 and another
I'm running a .NET remoting application built using .NET 2.0. It is a console
I am using Process.Start to fire-and-forget a long running console application within my ASP.NET
I'm running a console application under Windows on a remote remote computer. The following
How can I pipe between two separate console application running in different console windows?
I would like to know that If my console Application is running in debug
I am running a java application from the console on an HP-UX machine. In
I have just recently run into an issue running an asp.net web app under
I have a WAS hosted set of services configured to use net.tcp running under
We are deploying our ASP.Net MVC application to a windows 2008 R2 server, running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.