Hello. I am developing simple exercise formapp and I need to detect internet connection every 2 seconds.
#1. I do check internet connection like this
public static bool Connection()
{
bool connection;
try
{
System.Net.IPHostEntry objIPHE = System.Net.Dns.GetHostEntry("www.google.com");
connection = true;
}
catch
{
connection = false;
}
return connection;
}
On mainForm I have 2 labels. First Getting green when bool connection is true (when false red), and Second getting text "Online" or "Offline".
try
{
if (Internet.Connection() == true)
{
lblConnectivity.BackColor = System.Drawing.Color.Green;
lblConnectivityText.Text = "Online";
}
}
catch
{
lblConnectivity.BackColor = System.Drawing.Color.Red;
lblConnectivityText.Text = "Offline";
}
Is there any more intelligent and professional approach to detect connection to internet instead of pinging Google?
Well, despite this method works, it doesn't react if I will switch connection off. I have an idea to use threading and it's method sleep(200).
// Internet is the name of class where static method Connection lives
// Gives error because Error 1 'bool MailClient.Internet.Connection()' has the //wrong return type
Tread connectionThread=new Thread(new ThreadStart(Internet.Connection));
connectionThread.Start();
connectionThread.Sleep(200);
Looks like ThreadStart can accept only void. Is there any other approach to work with threads when I need even return type method?
Thanks for any useful responses.