2

I'm developing an application for Honeywell Dolphin 6100, a mobile computer with a barcode scanner that uses Windows CE 5.0 like OS.

I want to use a function which can check the network connection existence, I tried to use the code below but its too slow.

public static bool CheckForInternetConnection()
{
   string url = "http://www.Microsoft.com/";
   try
   {
       System.Net.WebRequest myRequest = System.Net.WebRequest.Create(url);
       System.Net.WebResponse myResponse = myRequest.GetResponse();
   }
   catch (System.Net.WebException)
   {
       return false;
   }
   return true;
}

Any one have an idea for a faster way to do that. I mention that I'm working with VS2008 on win7

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Mohamed Jihed Jaouadi
  • 1,427
  • 4
  • 25
  • 44
  • What do you mean by "network connection existence". Your existing code checks to make sure that you're connected to a network, can resolve IP addresses, can route outside your network, etc. Are any of those unneeded? – MNGwinn Apr 17 '13 at 16:51
  • I just want to check if my device is connected to a network or not – Mohamed Jihed Jaouadi Apr 18 '13 at 09:18

2 Answers2

2

Since you're not doing anything with the content of the response, there's really no reason to request it, and wait for the network transfer of all that data. You might try setting myRequest.Method = "HEAD", which will just return headers (assuming the web server supports it), but obviously still verify that you can communicate with the remote web server.

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.method.aspx

Ryan M
  • 2,072
  • 11
  • 14
0

Assuming availability on your version of the .net runtime, you can call the static System.Net.NetworkInformation.GetIsNetworkAvailable() method. There are scenarios where that'll return true and you won't be able to route to the outside world, but it'll tell you whether your device has a network interface that's marked as being up.

MNGwinn
  • 2,394
  • 17
  • 18