fipcurren88
fipcurren88

Reputation: 701

Deadlock Windows Phone 8 - UI Thread

I have two processes running on my UI thread. However, when I run the first, with BeginGetStream functionality, it enters the cycle and waits for its execution and returns the result when you are ready, but in the second run, through the BeginGetResponse functionality, this "die" there and the program does not continue and does not return me the value I want. Within these processes use the IasynResult.

I've tried:

Some code example:

First Request:

    int number = 123;
    1Request.Headers["Teste"] = string.Format("Teste{0}", number);
    IAsyncResult AsyncronousResult= null;

        try
        {
               await Task.Run(() => 1Request.BeginGetRequestStream(
               (IAsyncResult result) =>
               {
                   AsyncronousResult= result;
                   //handle.Set();
               }, 1Request));

            Task.WaitAny();

        }
        catch (Exception) { }

Second request:

    HttpWebRequest 2request = (HttpWebRequest)AsyncronousResult.AsyncState;
    IAsyncResult AsyncronousResult = null;

     Try 
    {
        await Task.Run(() => 2request.BeginGetResponse(
            (IAsyncResult result) =>
            {
                AsyncronousResult = result;

            }, 2request));
        Task.WaitAny();
    }

     catch
    {
    }

... I do not know what else to try ... Someone can help me.

Thank you.

Upvotes: 0

Views: 174

Answers (1)

Viacheslav Smityukh
Viacheslav Smityukh

Reputation: 5843

Any HttpWebRequest needs a free UI thread to complete request, but you block UI thread by the call of Task.WaitAny().

You must never block UI thread, especially to wait a result of the web request!

Upvotes: 1

Related Questions