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 7563275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:37:29+00:00 2026-05-30T13:37:29+00:00

I have problem with .Cancel and .Wait in tasks. It takes a lot of

  • 0

I have problem with .Cancel and .Wait in tasks. It takes a lot of memory and time.

Tasks:

using (CancellationTokenSource cancelSource = new CancellationTokenSource())
{
    CancelEventArgs args = new CancelEventArgs(false);
    Task task = Task.Factory.StartNew(() =>
    {
        try
        {
            action(args);
        }
        catch (Exception ex)
        {
            exception = ex;
        }
    }, cancelSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Current);

    bool success = task.Wait(maxRuntime);
    if (!success)
    {
        cancelSource.Cancel();
        return false;
    }
}

In C# threading I use .Abort for aborting threads. Here in tasks I use .Cancel. But I think that .Cancel takes a lot of memory and time and it is not the same as .Abort in threads. How can I decrease memory when using

bool success = task.Wait(maxRuntime);
if (!success)
{
    cancelSource.Cancel();
    return false;
}

Threading example:

Thread workerThread = new Thread(threadStart);

workerThread.Start();

bool finished = workerThread.Join(timeout);
if (!finished)
    workerThread.Abort();

return finished;
  • 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-05-30T13:37:31+00:00Added an answer on May 30, 2026 at 1:37 pm

    Canceling a task is fundamentally different from aborting a thread.

    Aborting a thread will raise a ThreadAbortException in the thread you aborted. So even if the code that is executed inside the thread isn’t aware of this mechanism it gets aborted.

    Canceling a task on the other hand will not raise any exception. Instead, the task needs to check whether CancellationToken.IsCancellationRequested is true and then actively throw an OperationCanceledException exception. That means that the task will not respond to the cancellation request if the code executed by the task is not checking for IsCancellationRequested.
    See this MSDN page for more info.

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

Sidebar

Related Questions

I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have problem with Uploadify: I log in the project using: FormsAuthentication.SetAuthCookie(myName, false); Then,
i have problem with closing saveFileDialog. when i click'cancel' window appears once again. here
I have problem with sortable <TD> 's. $(table tbody).sortable({ handle: td[tyhi='0'], cancel: td[tyhi='1'] });
I have a problem while I need to send data using uploadify script. For
I have a problem: I am programming an app that is using a searchBar
I have a problem now. I want to get wikipedia data using wikimeta. I
We have a problem deleting Appointments from Exchange using EWS. We try to delete
I have a problem. I upload some images from HDD to the website using
I have a problem with DataBinding in Winforms, Even though I click Cancel on

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.