I am kinda new to C# from a heavy java background, and still trying to figure out how exactly await and async works, here's whats confusing me a lot:
Suppose I have a function likes this (Hackish code below is used for experiments only):
public static bool CheckInternetConnection()
{
Task<bool> result = CheckInternetConnectionAsync();
return result.Result;
}
public async static Task<bool> CheckInternetConnectionAsync()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
SomeClass details = await ReturnARunningTask();
return details.IsSuccessful;
}
return false;
}
public Task<SomeClass> ReturnARunningTask()
{
return Task.Run(() => checkInternet());
}
in the main execution thread, if I call this:
CheckInternetConnection();
It will block indefinitely, my assumption is that control leaves CheckInternetConnectionAsync() at the "await" keyword and blocks on ".Result". When await resumes, the main thread is already blocked and stays blocked
The reason for my assumption is that I can see the task finish and return, but the code after await is never executed
however, if I do this:
bool result = Task.Run(() => CheckInternetConnection()).Result;
then the code after the await statement is executed and main thread does continue
My expectation was that it will also block, because the main thread would be blocked on the intermediate task,. the intermediate task would be blocked on the .Result
so..what made the difference? What in this case the code after await gets executed?