.Net8 WinForms Project Main Function
async Task<string> LoadData()
{
var httpClient = new HttpClient();
var response = await httpClient.GetStringAsync("http://www.google.com");
return response;
}
//var formStuck = new System.Windows.Forms.Form();//If This Code Uncomment,Application will be stuck
var httpResult = await LoadData();
Application.Run(new System.Windows.Forms.Form());
No Form object can be instantiated before the asynchronous method is called, otherwise it will get stuck.
But moving the code out of LoadData and removing the await keyword allows it to run.
var formStuck = new System.Windows.Forms.Form();
var httpClient = new HttpClient();
var response = httpClient.GetStringAsync("http://www.google.com").Result;
Application.Run(new System.Windows.Forms.Form());
Couldn’t instantiating the Form object cause some kind of mechanism change in the current thread?