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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:36:01+00:00 2026-05-23T16:36:01+00:00

When canceling a task that has a timeout (before the timeout has ended) using

  • 0

When canceling a task that has a timeout (before the timeout has ended) using a cancel token an exception is thrown. Example:

mytask.start();
bool didTaskRunInTime = mytask.wait(5 mins, _cancelToken);

Which means I cannot go on like below.

//was the task cancelled
if (_cancelToken.IsCancelRequested)
{
    // log cancel from user to file etc
}

if (didTaskRunInTime )
{
    int taskResult = myTask.Result;
    // log result to file
}
else if (!_cancelToken.IsCancelRequested)
{
    // Tell user task timed out , log a message etc
}

I will have to do all this in my catch block and my code is looking messy. What is the correct way to do this?

  • 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-23T16:36:02+00:00Added an answer on May 23, 2026 at 4:36 pm

    You could call Task.WaitAny with an array of just that task. Then you can act on the status of the task, however the method returns. Sample code:

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    
    class Test
    {
        static void Main()
        {
            Task sleeper = Task.Factory.StartNew(() => Thread.Sleep(100000));
    
            int index = Task.WaitAny(new[] { sleeper },
                                     TimeSpan.FromSeconds(0.5));
            Console.WriteLine(index); // Prints -1, timeout
    
            var cts = new CancellationTokenSource();
    
            // Just a simple wait of getting a cancellable task
            Task cancellable = sleeper.ContinueWith(ignored => {}, cts.Token);
    
            // It doesn't matter that we cancel before the wait
            cts.Cancel();
    
            index = Task.WaitAny(new[] { cancellable },
                                 TimeSpan.FromSeconds(0.5));
            Console.WriteLine(index); // 0 - task 0  has completed (ish :)
            Console.WriteLine(cancellable.Status); // Cancelled
        }
    }
    

    Note that if the task is faulted, you should “observe” the exception in order to avoid it going bang when it’s finalized 🙂

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

Sidebar

Related Questions

I have service object (Service A), that has some specific repetitive asynchronous task .
I have an application that has been getting strange errors when canceling out of
I'm having trouble canceling a background worker that has a Thread.Sleep(100) in it. private
Are there any known issues with canceling HttpWebRequest HTTP requests? We find that when
Using the SharePoint 2010 RC I am having issues canceling the deletion of a
Has anyone had experience canceling a workflow via webservices? Is it possible?
My application has modal dialogs in it, and also a feedback widget that sticks
UIScrollView has a canceling mechanism, where it cancels the touch of subviews when it
I'm calling a function in another thread using performSelectorInBackground. I'd like a canceling ability
I wrote a script that has no time limit and max_execution_time is 0 I

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.