0

My MainPage code-behind:

private void Button_Clicked(object sender, EventArgs e)
{
    Query().Wait();
    App.Current.MainPage = new Categories();
}

public async Task Query()
{
    restaurantsClient = (App.Current as App).restaurantsClient;

    try
    {
        var restaurantsNames = await restaurantsClient.GetCatalogsAsync(1);
    }
    catch (Exception ex)
    {
        var x = 0;
    }
}

I tried this code too but didn't work, happens the same problem:

async static Task GetRequest(String URL)
{
    using (HttpClient client = new HttpClient())
    {
        // As soon as I try to step over (or even into) this next line, it crashes.
        using (HttpResponseMessage response = await client.GetAsync(URL))
        {
            using (HttpContent content = response.Content)
            {
                string data = await content.ReadAsStringAsync();

                Console.WriteLine(data);
            }
        } 
    }
}

Rest-API in C#:

var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

When the project reach this line it just die without showing any error. If I do it in the browser it works. The API is running locally in my PC (Im using Conveyor to expose the API).

Ok I make a video to see better what Im talking about: https://youtu.be/ONKTipPsEXI As you can see after I click Next Step in response line stop executing the rest of the code.

1 Answer 1

1

That's because you're using .Wait() of a Task on the UI thread (Button.Clicked event is handled on the UI thread) causing a deadlock. The task is waiting for the UI thread to give it control and the UI thread is waiting for the task to complete. The solution to this is to add async modifier to your event handler and use await Query() instead of Query().Wait().

private async void Button_Clicked(object sender, EventArgs e)
{
    await Query();
    App.Current.MainPage = new Categories();
}

I'd also recommend reading this article by Stephen Cleary about this matter. Moreover, he's made a fabulous series (A Tour of Task) about C# tasks in general.

UPDATE:

After OP's question update and discussion in this answer's comments; he thinks that there's a problem because he can reach the end of GetCatalogsAsync(int) before the end of GetCatalogsAsync(int, CancellationToken). That's completely natural and is to be expected. Solution:

public async System.Threading.Tasks.Task<CatalogsInCategory> GetCatalogsAsync(int id)
{
    return await GetCatalogsAsync(id, System.Threading.CancellationToken.None);
}
Sign up to request clarification or add additional context in comments.

6 Comments

The problem of Android dying is solved but the problem of reaching the response line and don't execute the rest of the code remains.
I add a video to show what is happening in the question go take a check.
@LOLJovem I watched the video. What happens when you add a break-point on the first line inside the try (var headers_ = ...)? is it being hit after you press OK in the 'Telerik is trial' dialog?
But the var headers inside the try is below the response line so is never executed because when the project reach that line dont execute more code.
By 'but' you infer that it's being hit, and then you say it's never executed. So, is it being hit or not?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.