Reputation: 701
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)
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
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