I have a PC that loses the Internet connection every 5 minutes. (long to explain why).
On the background I'm running a C# timer every 10 mins that does very simple stuff:
WebBrowser bobo = new WebBrowser();
bobo.Navigate(url);
while(bobo.ReadyState != WebBrowserReadyState.Complete){Application.DoEvents();}
string responsestring = bobo.DocumentText.ToString();
bobo.Dispose();
// and then do some stuff with responsestring
It is very important to make sure that bobo webbrowser DID have an Internet connection when it was loading the page. How do I do that?
I tried "try-catch" statement, but it does not throw exceptions when there is no Internet.
I thought of doing the "loading complete" handler, but it will make my program very complex and use too much memory, so looking for other ways.
My latest solution is:
...
while(bobo.ReadyState != WebBrowserReadyState.Complete){Application.DoEvents();}
if (bobo.DocumentTitle == "Navigation Canceled"){throw new DivideByZeroException();}
...
It works fine for bobo browser. But when I work with responsestring - I create many other browsers (one by one) - and this solution does not work there.
Is there some other kind of test that I did not mention?
Solution found:
Thanks a lot.
I did not use your solution (it returns TRUE few seconds after connection is turned off).
But I found this:
[DllImport("wininet.dll", SetLastError = true)]
static extern bool InternetCheckConnection(string lpszUrl, int dwFlags, int dwReserved);
public static bool CanConnectToURL(string url)
{
return InternetCheckConnection(url, 1, 0);
}
It literally sends a PING to the URL, and returns TRUE if answer is received, otherwise it returns FALSE. Works perfect.