Internet Availability

  • Thread starter Finn Stampe Mikkelsen
  • Start date
F

Finn Stampe Mikkelsen

Hi

I need to have my program be Internet aware and constantly know, wether the internet i avaiable to it.. I have tried with this code:

[DllImport("wininet.dll")]private extern static bool InternetGetConnectedState( int out Description, int ReservedValue ) ;
//Creating a function that uses the API function...

public static bool IsConnectedToInternet( )
{

int Desc ;
return InternetGetConnectedState( out Desc, 0 ) ;
}

So far so good. It works okay. I've put the call in a timer event, which checks the availability every tick. When however the
internet connections breaks, the UI gets extremely slow and reactions to user actions is slowed down considerably...

Is there a better way to do this??

I've tried to delay my check for Inet to the actual code, where i need to know if there is a connection to the internet, but thsi
still slows down the handlings, so i must conclude that the above code has a long timeout when internet is not available.

I have tried using other methods of checking internet availability, such as creating an tcpclient and connecting,
System.Net.Dns.GetHostByName("www.google.com"), but all have the same result... When internet is not available, the software is
extremely slow...

Is there a way to do this, without the code slowing down??

/Finn
 
F

Finn Stampe Mikkelsen

"Peter Duniho" skrev i meddelelsen
[...]
So far so good. It works okay. I've put the call in a timer event, which
checks the availability every tick. When however the internet
connections breaks, the UI gets extremely slow and reactions to user
actions is slowed down considerably...

[...]
Is there a way to do this, without the code slowing down??

You cannot change how long the check itself takes without changing the timeout (either through configuration or a different
technique). But the fact is, the default timeout is likely to be reasonably realistic. After all, on the Internet it can actually
take some time to do things. And if you're going to make this check, you might as well try your best to avoid false negatives.

What you _can_ do is perform the check in a background thread rather than in your UI thread. Use a threading timer rather than the
one from System.Windows.Forms, and the timer event should wind up on a background thread automatically. Once the actual check is
done, you'll have to update your UI using Invoke(), but at least the UI won't block while you're waiting on the results.

Hi Pete

Thank you for youyr answer. I think i will use the seperate thread approach ;-)

The program has functions relying on internet connectivity and others that don't and i would like to be able to update th UI
accordingly, so the user knows what functionallity is available to him...

The code already does handle exceptions due to no connectivity at execution, but i would like to take it that step further and
simply disable the functions on the UI on top of the exception handling...

Thanks again for your input. Did not think of that background thread doing the job...

/Finn
 
T

tja

I realize this reply won't be much help to you, but I remember reading
somewhere that there was a way to check for status changes in internet
connectivity without having to do an unmanaged call. It seemed very
easy. I've never had occasion to use it, but it seemed very
straightforward. I don't know what it is, but I know that it's out
there. Good luck, brother.

tja
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top