internet connection

K

Konrad

Hi

I'am checking in separate thread does the connection with
internet is estabilished. But program freezes
for 2-3 secs. How to do that user
doesn't feel this checking?

thanks
Konrad
 
H

Herfried K. Wagner [MVP]

* "Konrad said:
I'am checking in separate thread does the connection with
internet is estabilished. But program freezes
for 2-3 secs. How to do that user
doesn't feel this checking?

How do you check if the connection is established?
 
K

Konrad

I found this method and it is quick

//Creating the extern function...

[DllImport("wininet.dll")]

private extern static bool InternetGetConnectedState(

out int Description, int ReservedValue ) ;



public static bool IsConnectedToInternet( )

{

int Desc ;

return InternetGetConnectedState( out Desc, 0 ) ;

}



and previous method

public static bool IsInternetConnected(string url)

{

bool FoundUrl = true;

try

{

WebRequest req;

WebResponse result;

Stream ReceiveStream;

Encoding encode;

StreamReader sr;

req = WebRequest.Create(url);

req.Timeout = 5000;

result = req.GetResponse();

ReceiveStream = result.GetResponseStream();

encode = System.Text.Encoding.GetEncoding("utf-8");

sr = new StreamReader(ReceiveStream, encode);

}

catch(System.Exception e)

{

FoundUrl = false;

}

return FoundUrl;


}

Thanks

Konrad
 
M

MuZZY

Konrad said:
I found this method and it is quick

//Creating the extern function...

[DllImport("wininet.dll")]

private extern static bool InternetGetConnectedState(

out int Description, int ReservedValue ) ;



public static bool IsConnectedToInternet( )

{

int Desc ;

return InternetGetConnectedState( out Desc, 0 ) ;

}



and previous method

public static bool IsInternetConnected(string url)

{

bool FoundUrl = true;

try

{

WebRequest req;

WebResponse result;

Stream ReceiveStream;

Encoding encode;

StreamReader sr;

req = WebRequest.Create(url);

req.Timeout = 5000;

result = req.GetResponse();

ReceiveStream = result.GetResponseStream();

encode = System.Text.Encoding.GetEncoding("utf-8");

sr = new StreamReader(ReceiveStream, encode);

}

catch(System.Exception e)

{

FoundUrl = false;

}

return FoundUrl;


}

Thanks

Konrad

How do you check if the connection is established?

Hi Konrad,

Well, i've seen in a few articles that using any windows api functions
to test internet connection isn't reliable anyway - at the most it will
say that you are connected, but it can't check either it's just a LAN
connection, or internet.

What i would do is try to establish a conection with a well nown web
site, say yahoo or google using System.Net.HttpWebRequest class.
I am not sure if it's a synchronous or asynchronous socket connection,
but anyway you can launch it in a separate thread.
I remember myself doing that back in Delphi couple years ago, and it
worked fine!

Andrey
 

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

Similar Threads

number of connection 5
ListView BackgroundImage 1
flickering 1
Threads 1
Windows XP Losing Internet Conection...... 0
Zero Upload Speed 6
Windows XP WIndows XP Wifi - No internet, but connected 4
Unable To Connect To Internet - IP Address Conflict 6

Top