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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:18:42+00:00 2026-05-31T18:18:42+00:00

I just realized that when i start a task from within a task and

  • 0

I just realized that when i start a task from within a task and call Task.Wait the new task will not be inlined, while calling Task.Result will always inline the task.

As we wrap our tasks with a RAII pattern (implemented in ExecuteWithCancel), inlining will reuse allocated resources and is preferable.

But we sometime want to wait a certain time and cancel the task after that.
The waiting code looks like this:

using (var cts = new CancellationTokenSource())
{
   // Task scheduler decides whether to execute synchronous or asynchronous
   var task = new Task<TResult>(() => ExecuteWithCancel<TResult>(cts.Token, nameOfTaskPerformer, arguments), cts.Token)
   if (timeout==TimeSpan.Zero || task.Wait(timeout)) // this creates an all or nothing timeout
      return task.Result;
   cts.Cancel();
   throw new TimeoutException("");
}

When timeout is TimeSpan.Zero the Task is inlined, otherwise it always uses another thread.

Is there an easy way to redesign this code to use inlining and waiting/timeout?

  • 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-31T18:18:43+00:00Added an answer on May 31, 2026 at 6:18 pm

    Pretty sure that’s not possible. Suppose you are running the following code on thread A:

    var task = Task.Factory.StartNew(() => Thread.Sleep(Timeout.Infinite));
    task.Wait(5000);
    

    If the task is inlined, thread A will block indefinitely – how will it wake up after the timeout?
    Looking at the reference source (Task.cs) we can see exactly that:

    internal bool InternalWait(int millisecondsTimeout, CancellationToken cancellationToken) 
    {
       ...
       // we will attempt inline execution only if an infinite wait was requested 
       // Inline execution doesn't make sense for finite timeouts and if a cancellation token was specified
       // because we don't know how long the task delegate will take. 
       if (millisecondsTimeout == Timeout.Infinite && !cancellationToken.CanBeCanceled &&
           WrappedTryRunInline() && IsCompleted) 
       {
           returnValue = true; 
       } 
       else
       { 
           returnValue = CompletedEvent.Wait(millisecondsTimeout, cancellationToken);
       }
    

    As per your question, in order to benefit from inlining with finite timeouts, you’d have to implement the timeout logic inside the task itself, perhaps something like:

    ExecuteWithCancel<TResult>(cts.Token, TimeSpan timeout, nameOfTaskPerformer, arguments)
    

    And then use a regular Wait() (or Result).

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

Sidebar

Related Questions

I just realized that i may not be following best practices in regards to
I just realized from an article in CACM that Doxygen works with Java (and
I just realized that I'm going to have to start aliasing my database calls
EDIT I just realized that I must have had a massive brain fart while
I just realized that in some place in my code I have the return
I just realized that I need to synchronize a significant amount of data collection
I just realized that the method Element.getElementsByTagName(someTagName) returns a nodelist of all elements in
I am testing my webpage on a server using preview dns. Just realized that
Just downloaded the Visual Studio 2010 Premium and realized that can't compile the project,
I just starting reading about SEO and realized that I should change my GET

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.