Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to test if Webbrowser gets a connection error when navigating to a new URL?
    text
    copied!<p>I have a PC that loses the Internet connection every 5 minutes. (long to explain why).<br/> On the background I'm running a C# timer every 10 mins that does very simple stuff:</p> <pre><code>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 </code></pre> <p>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?</p> <p>I tried "try-catch" statement, but it does not throw exceptions when there is no Internet.</p> <p>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.</p> <p>My latest solution is:</p> <pre><code>... while(bobo.ReadyState != WebBrowserReadyState.Complete){Application.DoEvents();} if (bobo.DocumentTitle == "Navigation Canceled"){throw new DivideByZeroException();} ... </code></pre> <p>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.</p> <p>Is there some other kind of test that I did not mention?</p> <p>Solution found:<br/> Thanks a lot.<br/> I did not use your solution (it returns TRUE few seconds after connection is turned off).<br/> But I found this:</p> <pre><code>[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); } </code></pre> <p>It literally sends a PING to the URL, and returns TRUE if answer is received, otherwise it returns FALSE. Works perfect.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload