I have async function which waits async Task<string>
, but before that it should execute sync process(which is debug.log
). However, instead main thread freezes until await return something.
Button's function which calls that async function
private void Register()
{
LogRegHandler.logRegHandler.RegisterProcess(RegisterNamefield.text, RegisterEmailfield.text, RegisterPassfield.text)
;
}
Async function which freeze main thread
public async void RegisterProcess(string name,string email, string password)
{
Debug.Log("before Await");
RegLogUIController.regLogUIController.CreateLoadingPanel("dil controller editleyecek");
string registerResult = await registerLogIn.RegisterUser(name,email, password);
Debug.Log("after Await");
}
Awaited process
public async Task<string> RegisterUser(string name, string email, string password)
{
string jsonResponse;
try
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(serverAddress.serverMainAddress + "RegisterApi/register");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = String.Format("{{\"name\":\"{0}\",\"email\":\"{1}\",\"password\":\"{2}\"}}",name,email,password);
streamWriter.Write(json);
}
HttpWebResponse response = (HttpWebResponse)(await httpWebRequest.GetResponseAsync());
StreamReader reader = new StreamReader(response.GetResponseStream());
jsonResponse = reader.ReadToEnd();
}
catch (Exception)
{
return "";
}
return jsonResponse;
}
log
before Await
after Await
//they pop-up at the same time after freezing ends
What I am doing wrong ?