0

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:

  • Convert to a Task (Task.Run)

  • Allocate the process a new thread (Thread x = new Thread ())

  • Set the parameter ConfigureAwait (ConfigureAwait (      continueOnCapturedContext: false), ConfigureAwait (false));

  • Convert to asyncronos methods and apply the "await"

  • Use the AutoResetEvent to wait for the process to complete and do not proceed.

  • Placement thread to sleep (Thread.Sleep)

  • Use of Thread.Start, Thread.Join

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.

fipcurren88
  • 701
  • 5
  • 26

1 Answers1

1

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!

Viacheslav Smityukh
  • 5,652
  • 4
  • 24
  • 42
  • Ok.'re Right. But I need, the response to the first request to proceed to the second request. However, I need the answer to the second request to continue the program. I removed Task.WaitAny (); but still does not work. – fipcurren88 Apr 14 '15 at 14:41