4

I do asynchronous requests in LoadState method of a certain Page. I use HttpClient to make a request and I expect the splashscreen to go away while I await the result.

If I am not connected to any networks, the splashscreen immediately goes away and I get a blank page because the request obviously didn't happen.

But if I am connected to a network but have connectivity issues (for example, I set a wrong IP address) it seems to start a request and just block.

My expectation was that the HttpClient would realize that it cannot send a request and either throw an exception or just return something.

I managed to solve the issue of blocking by setting a timeout of around 800 milliseconds, but now it doesn't work properly when the Internet connection is ok. Is this the best solution, should I be setting the timeout at all? What is the timeout that's appropriate which would enable me to differentiate between an indefinitely blocking call and a proper call that's just on a slower network?

I could perhaps check for Internet connectivity before each request, but that sounds like an unpredictable solution...

EDIT: Now, it's really interesting. I have 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's always fine. BUT it's also fine without Task.Run() if there is no Internet access but the network access is not "Limited" (it says that the IPv4 connectivity is "Internet access" although I cannot open a single website in a browser and no data is returned from the web service. It just throws System.Net.Http.HttpRequestException which was something I was expecting in the first place) Only blocks when the network connection is Limited.

Igor Ralic
  • 14,975
  • 4
  • 43
  • 51
  • 1
    How do you issue the request? Perhaps something in the way you do it blocks the UI thread? Can you try starting it from a background thread, eg. using `Task.Run()`? – Filip Skakun Jan 21 '13 at 19:41
  • Thanks, I tried that and edited my question a bit with more details. – Igor Ralic Jan 21 '13 at 20:15
  • Check out my answer on a similar question, it probably won't satisfy you but will give a possible reason for this problem http://stackoverflow.com/questions/14526761/await-blocks-the-ui-thread/14530929#14530929 – DVD Jan 29 '13 at 17:39
  • If the Task.Run() solution works, then why just not always use it? Probably SendAsync isn't implemented correctly and it blocks the calling thread in certain situations when in fact it should do whatever it does asynchronously. – Filip Skakun Jan 30 '13 at 21:00
  • This may be expected in `Debug` mode. Have you tried it on `Release` mode? – kiewic Aug 27 '14 at 17:29

1 Answers1

1

What if instead of setting a timeout, you checked the connection status using

public static bool IsConnected
    {
        get
        {
            return NetworkInformation.GetInternetConnectionProfile() != null;
        }
    }

This way if IsConnected, then you make the call; otherwise, ignore it.

I'm not sure if you are running this in App.xaml.cs? I've found requests made in that class can be fickle and it may be best to move the functionality to an extended splash screen to ensure the application makes it all the way through the activation process.

http://msdn.microsoft.com/en-us/library/windows/apps/xaml/Hh868191(v=win.10).aspx

Chris Woolum
  • 2,854
  • 20
  • 20