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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:28:22+00:00 2026-06-12T09:28:22+00:00

I created an ASP WebApplication using Visual Studio 2012. If I modify the default

  • 0

I created an ASP WebApplication using Visual Studio 2012.

If I modify the default page as follows:

public partial class _Default : Page
{
    static async Task PerformSleepingTask()
    {
        Action action = () =>
        {
            Thread.Sleep(TimeSpan.FromSeconds(0.5));
            int dummy = 3; // Just a nice place to put a break point
        };
        await Task.Run(action);
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        Task performSleepingTask = PerformSleepingTask();
        performSleepingTask.Wait();
    }
}

On the call to performSleepingTask.Wait() it hangs indefinitely.


Interestingly, if I set this in the web.config:

<appSettings>

    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="false" />
</appSettings>

Then it does work. The Wait function waits for the sleeping to finish on a different thread, then continues.


Can somebody explain:

  • Why does it hang?
  • Why do they have something called TaskFriendlySynchronizationContext? (Given that it causes tasks to hang, I wouldn’t call it “friendly”)

  • Is there a “best practice” for invoking async methods from page handler methods?

Here’s an implementation I came up with which works, but it feels like clumsy code:

    protected void Page_Load(object sender, EventArgs e)
    {
        ManualResetEvent mre = new ManualResetEvent(false);
        Action act = () =>
        {
            Task performSleepingTask = PerformSleepingTask();
            performSleepingTask.Wait();
            mre.Set();
        };
        act.BeginInvoke(null, null);
        mre.WaitOne(TimeSpan.FromSeconds(1.0));
    }
  • 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-12T09:28:22+00:00Added an answer on June 12, 2026 at 9:28 am

    Why does it hang?

    The Task representing PerformSleepingTask is trying to resume after its await so that PerformSleepingTask can return. It is trying to re-enter the ASP.NET request context, which is blocked by the call to Wait. This causes a deadlock, as I expound on my blog.

    To avoid the deadlock, follow these best practices:

    1. Use async all the way down. Don’t block on async code.
    2. Use ConfigureAwait(false) in your “library” methods.

    Why do they have something called TaskFriendlySynchronizationContext? (Given that it causes tasks to hang, I wouldn’t call it “friendly”)

    TaskFriendlySynchronizationContext uses the new AspNetSynchronizationContext (the .NET 4.0 TaskFriendlySynchronizationContext has been renamed LegacyTaskFriendlySynchronizationContext), and it also uses a new, async-aware pipeline.

    I’m not 100% sure about this one, but I suspect that the reason Page_Load works for the legacy SyncCtx is that the old pipeline wasn’t putting the SyncCtx in place yet. I’m not sure why it would act this way, though (unless Page.Async was false).

    Is there a “best practice” for invoking async methods from page handler methods?

    Absolutely. You can either just make your event handlers async void, or use RegisterAsyncTask(new PageAsyncTask(...));. The first approach is easier but the second approach is favored by the ASP.NET team.

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

Sidebar

Related Questions

I created a new asp.net web application using the template that visual studio has.
I'm currently using Visual Studio 2010 and have created a 'ASP.NET MVC 3 Web
I have created a web application using ASP.NET MVC3 in Visual studio (no SQL
I have created a Visual Studio 2010 ASP.NET Web Application. With it I have
I have created a Visual Studio 2010 ASP.NET Web Application. I've noticed that if
I create a ASP.net MVC 4 project by using Visual Studio 2010 IDE. I
I am wrapping up an application using ASP.NET in Visual Studio 2008. I am
When I create a ASP.NET Web Application (using .net framework 4) in visual studio
I am using Visual Studio 2008, and I had an ASP.NET dev configuration working
I'm developing an ASP.NET forms webapplication using C#. I have a method which creates

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.