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
etc....
I have essentially three functions(Function1,Function2 and Function3). Initially,I only use the Function2 and function3 and everything ok. Then, in anext order,where I do not want the UI thread lock,it blocks,and this time chamos the three functions I have, but the application blocks me when I make the request for BeginGetResponse in Function2.Not out there. (Putting the whole project in asynchronous is very costly,so this option will be the last.) I do not knowwhat elsecan I do...:( Some code example:
Function 1:
public void Function1()
{
{
HttpWebRequest requestHttp = (HttpWebRequest)HttpWebRequest.CreateHttp(new Uri(serverUrl, UriKind.Absolute));
string RString = string.Format("rftoken=123456789");
AutoResetEvent handler = new AutoResetEvent(false);
IAsyncResult TestResult= null;
try
{
requestHttp.BeginGetRequestStream(
(IAsyncResult result) =>
{
TestResult= result;
handler.Set();
}, requestHttp );
handler.WaitOne();
}
catch (Exception) { }
return Function2(TestResult, RString );
}
}
--------FUNCTION 2 ----------------------------------------------------------------------------------------
private Example Function2(IAsyncResult TestResult2, string RString2)
try
{
HttpWebRequest request = (HttpWebRequest)TestResult2.AsyncState;
Stream strm = request.EndGetRequestStream(TestResult2);
StreamWriter writer = new StreamWriter(strm);
writer.Write(RString2);
writer.Dispose();
}
catch
{}
AutoResetEvent handler = new AutoResetEvent(false);
IAsyncResult Async= null;
try
{
requestHttp.BeginGetRequestStream(
(IAsyncResult result) =>
{
Async= result;
handler.Set();
}, requestHttp );
handler.WaitOne();
}
catch (Exception) { }
return Function3(Async);
}
}
------------FUNCTION 3 ---------------------------------------------------------------------------------------
private Example Function3 (IAsyncResult Async3)
{....}
... I do not know what else to try ... Someone can help me.
Thank you.