I execute asynchronous requests in the LoadState method of a specific page. I use HttpClient to make a request and I expect the popup to disappear while I wait for the result.
If I am not connected to any networks, the splash screen immediately disappears, and I get a blank page because the request clearly did not happen.
But if I am connected to the network but have connection problems (for example, I am setting the wrong IP address), it seems to start the request and just block.
I expected HttpClient to understand that it could not send a request and either throw an exception or just return something.
I managed to solve the blocking problem by setting a timeout of about 800 milliseconds, but now it does not work properly when the Internet connection is ok. Is this the best solution, should I set a timeout at all? What is the timeout that suits me so that I can distinguish between an infinitely blocking call and the right call, which is only on a slower network?
Maybe I can check my internet connection before every request, but that sounds like an unpredictable solution ...
EDIT: Now this is really interesting. I tried again, and it blocks at this point:
var rd = await httpClient.SendAsync(requestMsg);
If I use Task.Run (), as suggested in the comments, and get a new thread, then it is always beautiful. BUT this is also excellent without Task.Run () if there is no Internet access, but network access is not “limited” (it says that IPv4 connection is “Internet access”, although I cannot open one web the site is in the browser and no data is returned from the web service. It just throws a System.Net.Http.HttpRequestException, which was something that I expected in the first place). Blocks only when the network connection is limited.