How can i check if the internet connection is active or not and to give a message to the user every

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
My application is downloading rain radar images every 30 seconds.
And the internet connection is made every 5 minutes.
The problem now is when the internet connection with the website is not exist if the site return not found 404 or any other problem with the site so the application cant connect with the site so the application is hanging freezing untill there is a connection.
It also happen if i take out the internet cable from the modem if im not wrong.
This is the class im using for the internet connection/s im testing internet connection with the site it self and also the internet connection physical line at least thats what i want to test the connection to the site and if the internet connection is
down for some reason or if the line physical is not connected(cable out).

Im not sure this code is any good:

<pre class="prettyprint using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using DannyGeneral;
using System.Runtime.InteropServices;

namespace mws
{
public partial class Network_Test : Form
{
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
public HttpWebResponse resp;
public Network_Test()
{
InitializeComponent();
}

public static bool IsConnected()
{
int Description;
return InternetGetConnectedState(out Description, 0);
}


// Checking if network is available means if the intenet cable is connected to the modem ( not if the the dialer to the internet is disconnected ).
public bool network_connection_test()
{
bool result = false;
try
{
result = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();


}
catch
{

result = false; // host not reachable

}
return result;

}





public bool http_test()
{
try
{

string website_address;
website_address = "http://www.ims.gov.il";
// req = (HttpWebRequest)WebRequest.Create("http://www.ims.gov.il");

WebRequest myWebrequest = WebRequest.Create(website_address);
myWebrequest.Timeout = 3000;
resp = (HttpWebResponse)myWebrequest.GetResponse();

if (resp.StatusCode.ToString().Equals("OK"))
{

return true;
}
else
{

MessageBox.Show("Connecting to the internet failed , please check your internet connection or the website " + website_address);
return false;
}
}
catch (Exception exc)
{
// Logger.Write("Connection Error: " + exc + Environment.NewLine+Environment.NewLine);
Logger.Write("Connection Error: " + exc + Environment.NewLine);
// MessageBox.Show("Connecting to the internet failed: "+exc);
return false;
}
finally
{
if (resp != null) resp.Close();
}

}
public bool satellite_test()
{
try
{

string website_address;
website_address = "http://www.sat24.com/image.ashx?country=eu&type=last&time=&sat=";
// req = (HttpWebRequest)WebRequest.Create("http://www.ims.gov.il");

WebRequest myWebrequest = WebRequest.Create(website_address);
myWebrequest.Timeout = 3000;
resp = (HttpWebResponse)myWebrequest.GetResponse();

if (resp.StatusCode.ToString().Equals("OK"))
{
// MessageBox.Show("its connected.");
return true;
}
else
{

MessageBox.Show("Connecting to the internet failed , please check your internet connection or the website " + website_address);
return false;
}
}
catch (Exception exc)
{
// Logger.Write("Connection Error: " + exc + Environment.NewLine+Environment.NewLine);
Logger.Write("Connection Error: " + exc + Environment.NewLine);
// MessageBox.Show("Connecting to the internet failed: "+exc);
return false;
}
finally
{
if (resp != null) resp.Close();
}
}

}

}



[/code]
Now in Form1 i have a timer:

This is Timer1 tick event where im doing the downloadings of both satellite images and radar images ever 30 seconds:

<pre class="prettyprint private void timer1_Tick(object sender, EventArgs e)
{
try
{
numbers_radar = Convert.ToInt64(numericUpDown1.Value);
numbers_satellite = Convert.ToInt64(numericUpDown2.Value);
if (numbers_radar <= 0 || numbers_satellite <= 0)
{
timer1.Stop();
button1.Enabled = true;
numericUpDown1.Enabled = true;
numericUpDown2.Enabled = true;
MessageBox.Show("Value cannot be zero or below zero");
numericUpDown1.Text = "";
numericUpDown2.Text = "";
}
else
{
Numbers_Timer_radar = Numbers_Timer_radar + 1;
Numbers_Timer_satellite = Numbers_Timer_satellite + 1;

if (Numbers_Timer_radar >= numbers_radar)
{
try
{
filesdownload();
number_of_files_in_current_directory = Directory.GetFiles(sf, "radar*.png");
label13.Text = "Current Number Of Files In The Selected Directory Are: " + number_of_files_in_current_directory.Length;
button1.Enabled = false;
Numbers_Timer_radar = 0;
}
catch (Exception Local_Timer1_Exceptions)
{
Logger.Write("Timer1 Errors : " + Local_Timer1_Exceptions);
}
}
if (Numbers_Timer_satellite >= numbers_satellite)
{
satellite_downloads();
number_of_satellite_files_in_current_directory = Directory.GetFiles(satellite_dir, "satellite*.png");
label8.Text = "Current Number Of Files In The Selected Directory Are: " + number_of_satellite_files_in_current_directory.Length;
button1.Enabled = false;
Numbers_Timer_satellite = 0;
}
time_left_radar = numbers_radar - Numbers_Timer_radar;
time_left_satellite = numbers_satellite - Numbers_Timer_satellite;
label21.Text = time_left_radar.ToString();
label22.Text = time_left_satellite.ToString();
if (time_left_radar == numericUpDown1.Value)
{
//settingsmenu.speaker.Rate = -1;
//settingsmenu.speaker.SpeakAsync("the next update for a new radar image will be in" + time_left_radar + " Seconds");
}
if (time_left_satellite == numericUpDown2.Value)
{
//settingsmenu.speaker.Rate = -1;
//settingsmenu.speaker.SpeakAsync("the next update for a new satellite image will be in" + time_left_satellite + " Seconds");
}
label1.Text = "Next image radar will be download in: ";
label5.Text = "Next image satellite will be download in: ";
}

}
catch (Exception General_Exceptions)
{
Logger.Write("Show if numbers have wrong string input" + numbers_radar);
Logger.Write("Show if numbers have wrong string input" + numbers_satellite);
timer1.Stop();
Logger.Write("Exception: " + General_Exceptions + Environment.NewLine);
button1.Enabled = true;
numericUpDown1.Enabled = true;
numericUpDown2.Enabled = true;
Numbers_Timer_radar = 0;
Numbers_Timer_satellite = 0;
}

}

private void filesdownload()
{
fileDownloadRadar();
}[/code]
This is fileDownloadRadar() and satellite_downloads()

<pre class="prettyprint private void fileDownloadRadar()
{
if (Client.IsBusy == true)
{
Client.CancelAsync();
}
else
{

Client.DownloadFileAsync(myUri, Path.Combine(combinedTemp)); // If debugButtonSwitch is true then its never download the file so its never get into the download file completed event to check it to solve \

}
}
private void satellite_downloads()
{
if (SatelliteClient.IsBusy == true)
{
SatelliteClient.CancelAsync();
}
else
{
SatelliteClient.DownloadFileAsync(mySatelliteUri, Path.Combine(temp_dir + satellite_file_name));
}
}[/code]
This is timer10 tick event where im doing the internet connection tests:

<pre class="prettyprint private void timer10_Tick(object sender, EventArgs e)
{
if (fdt.network_connection_test() == false)
{
label19.Enabled = true;
label19.Visible = true;
label19.ForeColor = Color.Black;
label19.Text = "Internet Connection Is Not Active";
}
else
{
if (fdt.http_test() == false)
{
label19.Enabled = true;
label19.Visible = true;
label19.ForeColor = Color.Black;
label19.Text = "Internet Connection Is Not Active";
}
else
{
label19.Enabled = true;
label19.Visible = true;
label19.ForeColor = Color.Green;
label19.Text = "Internet Connection Is Active";
label19.Enabled = true;
label19.Visible = true;
label19.ForeColor = Color.Green;
label19.Text = "Internet Connection Is Active";
}
}
}[/code]
Timer10 interval is set to: 300000
Im also using this internet connection/s once the program is loading.
The problem is that sometimes when there is a problem with the internet connection im not sure if its a problem with the website or the internet connection physicaly but then the application is hanging then freezing untill there is an internet connection.

The question how can i solve it so when there is no internet connection or/and a problem with the website the application will keep workin regular and just will report ot the user that there is or there isnt internet connection ? <hr class="sig danieli

View the full article
 
Back
Top