Click here to Skip to main content
15,116,253 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
Hi there,

I've created an application which mostly depends on internet connection.So i want to add a feature so that whenever user's system goes offline the application will be forced quit. I'd always monitor internet connectivity.

Thanks for help
Posted
Updated 1-Apr-11 18:06pm
v2

All you have to do is Google for ".NET detect internet connection[^]" and you'll find tons of examples.
   
Comments
DEB4u 1-Apr-11 23:36pm
   
Yes i got a function which checks internet connectivity...but i want that this should run always...
Here is another link that could help you - http://www.andreas-kraus.net/blog/check-if-the-internet-connection-state-is-active/[^].

You can rerun this code at multiple intervals by using a timer.
   
Comments
DEB4u 2-Apr-11 14:57pm
   
I'm having a function to check internet connectivity...as it has return type so can't use as a thread...Needs some help as new to .net...
Abhinav S 3-Apr-11 3:17am
   
You could use a background worker with an e.result even that you could use for further processing.
See http://www.albahari.com/threading/part3.aspx.
DEB4u 3-Apr-11 6:55am
   
this link does not work..
Abhinav S 3-Apr-11 7:34am
   
Hope you did not include the full stop with the link.
DEB4u 3-Apr-11 8:22am
   
ya u r right... let me go through it...
/// <summary>
/// Method used to check for internet connectivity by piging
/// varoaus websites and looking for the response.
/// </summary>
/// <returns>True if a ping succeeded, False if otherwise.</returns>
/// <remarks></remarks>
public bool isConnectionAvailable()
{
//build a list of sites to ping, you can use your own
string[] sitesList = { "www.google.com", "www.microsoft.com" , "www.psychocoder.net" };
//create an instance of the System.Net.NetworkInformation Namespace
Ping ping = new Ping();
//Create an instance of the PingReply object from the same Namespace
PingReply reply;
//int variable to hold # of pings not successful
int notReturned = 0;
try
{
//start a loop that is the lentgh of th string array we
//created above
for (int i = 0; i <= sitesList.Length; i++)
{
//use the Send Method of the Ping object to send the
//Ping request
reply = ping.Send(sitesList[i], 10);
//now we check the status, looking for,
//of course a Success status
if (reply.Status != IPStatus.Success)
{
//now valid ping so increment
notReturned += 1;
}
//check to see if any pings came back
if (notReturned == sitesList.Length)
{
_success = false;
//comment this back in if you have your own excerption
//library you use for you applications (use you own
//exception names)
//throw new ConnectivityNotFoundException(@"There doest seem to be a network/internet connection.\r\n
//Please contact your system administrator");
//use this is if you don't your own custom exception library
throw new Exception(@"There doest seem to be a network/internet connection.\r\n
Please contact your system administrator");
}
else
{
_success = true;
}
}
}
//comment this back in if you have your own excerption
//library you use for you applications (use you own
//exception names)
//catch (ConnectivityNotFoundException ex)
//use this line if you don't have your own custom exception
//library
catch (Exception ex)
{
_success = false;
_returnMessage = ex.Message;
}
return _success;
}
//Example Useage
If(!(isConnectionAvailable))
{
//then do something
}
{
//then do something
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900