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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:14:47+00:00 2026-06-06T19:14:47+00:00

public class HomeController : Controller { public ActionResult Index() { Task<string> t = DownloadStringTaskAsync(new

  • 0
public class HomeController : Controller
{
    public ActionResult Index()
    {
        Task<string> t = DownloadStringTaskAsync(new Uri("http://www.google.com/search?q=aspnet"));
        t.Wait();
        string res = t.Result;

        return View((object)res);
    }

    Task<string> DownloadStringTaskAsync(Uri address)
    {
        TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
        WebClient wc = new WebClient();
        wc.DownloadStringCompleted += (s, e) => tcs.SetResult(e.Result);
        wc.DownloadStringAsync(address);
        return tcs.Task;
    }
}

The action will never return and the request will simply timeout.

We are using TPL tasks for all asynchronous operations and had to wrap some of WebClient’s methods until we switch to framework 4.5.

The above method DownloadStringTaskAsync is a simplified version of one of those wrappers.

What I can’t figure out is why waiting on the task deadlocks the thread in aspnet and works fine in console or desktop applications.

Here’s how the deadlocked threads look like.

request thread

[In a sleep, wait, or join]
mscorlib.dll!System.Threading.Monitor.Wait(object obj, int millisecondsTimeout, bool exitContext) + 0x18 bytes
mscorlib.dll!System.Threading.ManualResetEventSlim.Wait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) + 0x264 bytes
mscorlib.dll!System.Threading.Tasks.Task.InternalWait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) + 0x166 bytes
mscorlib.dll!System.Threading.Tasks.Task.Wait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) + 0x41 bytes
mscorlib.dll!System.Threading.Tasks.Task.Wait() + 0xb bytes
TCSProblem.DLL!TCSProblem.Controllers.HomeController.Index() Line 16 + 0xa bytes

WebClient thread

[In a sleep, wait, or join]
mscorlib.dll!System.Threading.Monitor.Enter(object obj, ref bool lockTaken) + 0x14 bytes
System.Web.dll!System.Web.AspNetSynchronizationContext.CallCallback(System.Threading.SendOrPostCallback callback, object state) + 0x53 bytes
System.Web.dll!System.Web.AspNetSynchronizationContext.Post(System.Threading.SendOrPostCallback callback, object state) + 0x9 bytes
System.dll!System.ComponentModel.AsyncOperation.Post(System.Threading.SendOrPostCallback d, object arg) + 0x29 bytes
System.dll!System.Net.WebClient.PostProgressChanged(System.ComponentModel.AsyncOperation asyncOp, System.Net.WebClient.ProgressData progress) + 0x25c bytes

  • 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-06T19:14:48+00:00Added an answer on June 6, 2026 at 7:14 pm

    WebClient, unfortunately, captures the current synchronization context and raises the completion event by posting to it. Because of that the completion action only runs after you return from your action method which will never happen. This means, you either…

    1. …need to wrap your call to DownloadStringAsync in a Task.Factory.StartNew in order to switch to a thread that has no synccontext
    2. …need to set SynchronizationContext.Current to null before calling, and restore it afterwards
    3. …use an asynchronous MVC action
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Controller: public class HomeController : Controller { Models.MakaleSitesiDBEntities entity = new Models.MakaleSitesiDBEntities(); public ActionResult
public class HomeController : Controller { public ActionResult Index() { return View(); } /*
I have the following action: public class HomeController : Controller { public ActionResult Index(int?
Consider: public class HomeController : Controller { private IDependency dependency; public HomeController(IDependency dependency) {
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
I have a controller in C# using the ASP.Net MVC framework public class HomeController:Controller{
I have a very simple MVC app at this point: Controller: public class HomeController
i have this timer in c#: public class HomeController : Controller { public int
Let's say I have an async controller like that below ~ public class HomeController
public class MyClass { public string MyProperty{ get; set; } Now, I would like

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.