70

I created Windows Phone 8.1 project and I am trying to run async method GetResponse<T>(string url) on button click and waiting for the method to finish, but method is never finishing. Here is my code:

private void Button_Click(object sender, RoutedEventArgs 
{
      Task<List<MyObject>> task = GetResponse<MyObject>("my url");
      task.Wait();
      var items = task.Result; //break point here
}

public static async Task<List<T>> GetResponse<T>(string url)
{
    List<T> items = null;
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

    var response = (HttpWebResponse)await Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
    try
    {
        Stream stream = response.GetResponseStream();
        StreamReader strReader = new StreamReader(stream);
        string text = strReader.ReadToEnd();
        items = JsonConvert.DeserializeObject<List<T>>(text);
    }
    catch (WebException)
    {
        throw;
    }
    return items;
}

It will hang on task.Wait().

I changed my button click method to async and used await before the async method and I get the result(await GetResponse<string>("url")). What is wrong with Task<List<string>> task = GetResponse<string>("url")? What am I doing wrong?

Thanks for the help!

Andrew Truckle
  • 13,595
  • 10
  • 45
  • 105
Doniyor Niyozov
  • 1,026
  • 1
  • 8
  • 16
  • 7
    Just a small note, it is a general convention that `async` methods are suffixed with `Async`, i.e. your method name would be `GetResponseAsync`. – Default Feb 19 '15 at 08:46
  • Calling `task.Wait();` is redundant here. `task.Result` will already call `task.Wait()` when necessary. – Peter Bruins Mar 05 '21 at 19:16

3 Answers3

116

You're the victim of the classic deadlock. task.Wait() or task.Result is a blocking call in UI thread which causes the deadlock.

Don't block in the UI thread. Never do it. Just await it.

private async void Button_Click(object sender, RoutedEventArgs 
{
      var task = GetResponseAsync<MyObject>("my url");
      var items = await task;
}

Btw, why are you catching the WebException and throwing it back? It would be better if you simply don't catch it. Both are same.

Also I can see you're mixing the asynchronous code with synchronous code inside the GetResponse method. StreamReader.ReadToEnd is a blocking call --you should be using StreamReader.ReadToEndAsync.

Also use "Async" suffix to methods which returns a Task or asynchronous to follow the TAP("Task based Asynchronous Pattern") convention as Jon says.

Your method should look something like the following when you've addressed all the above concerns.

public static async Task<List<T>> GetResponseAsync<T>(string url)
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    var response = (HttpWebResponse)await Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);

    Stream stream = response.GetResponseStream();
    StreamReader strReader = new StreamReader(stream);
    string text = await strReader.ReadToEndAsync();

    return JsonConvert.DeserializeObject<List<T>>(text);
}
Andrew Truckle
  • 13,595
  • 10
  • 45
  • 105
Sriram Sakthivel
  • 67,773
  • 7
  • 96
  • 172
27

This is what's killing you:

task.Wait();

That's blocking the UI thread until the task has completed - but the task is an async method which is going to try to get back to the UI thread after it "pauses" and awaits an async result. It can't do that, because you're blocking the UI thread...

There's nothing in your code which really looks like it needs to be on the UI thread anyway, but assuming you really do want it there, you should use:

private async void Button_Click(object sender, RoutedEventArgs 
{
    Task<List<MyObject>> task = GetResponse<MyObject>("my url");
    var items = await task;
    // Presumably use items here
}

Or just:

private async void Button_Click(object sender, RoutedEventArgs 
{
    var items = await GetResponse<MyObject>("my url");
    // Presumably use items here
}

Now instead of blocking until the task has completed, the Button_Click method will return after scheduling a continuation to fire when the task has completed. (That's how async/await works, basically.)

Note that I would also rename GetResponse to GetResponseAsync for clarity.

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • I've added `async` to my button click event handler and am `await`ing the async task but the async task is still running on my main thread and blocking the Windows Form UI. Any ideas why when I'm following the above example? Thanks – Chris Walsh Nov 27 '20 at 11:42
  • 1
    @ChrisWalsh: Yes, the async task *would* still run on your main thread. If it's truly asynchronous (e.g. doing asynchronous IO) that should be fine. It's *not* fine if that task performs blocking operations. I suggest you ask a new question with a complete example. – Jon Skeet Nov 27 '20 at 11:46
-3

use below code

 Task.WaitAll(Task.Run(async () => await GetResponse<MyObject>("my url")));
Mahesh
  • 2,499
  • 1
  • 27
  • 30
  • 8
    `Task.WaitAll` will block in the same way as `task.Wait`. – Simon MᶜKenzie Oct 19 '16 at 05:31
  • question was "button click and waiting for the method to finish" so above code snip will wait until method to finish. – Mahesh Oct 19 '16 at 22:39
  • This is EXACTLY what I needed.. THank you. I have a user clicking a button.. that button does different things... one of which MAY end up needing to call an Asyc Process. Needed to know how to halt.. allow that process to finish, so my current thread can continue on to the next step. Works perfect! – da_jokker May 13 '21 at 20:41