Reputation: 2924
I wrote a small console app which connects with a remote site, downloads some data and process it. As it is network operation so I want my application to be intelligent enough to go to pause state when there is no internet connection. If internet connection gets available it should resume its working.
What I done so far is if I run application it starts downloading data from remote site. If I disconnects internet connection it behaves correctly by displaying appropriate info. But as soon as network connection is up it resumes and only download data for one iteration of while loop.
Here is the code:
class Program
{
static bool networkIsAvailable = false;
static StreamWriter writer = null;
static int i = 1;
static string URI = "http://xxxxxxxxxxxxxxxxxxxxxxxxxxx/" + i + "/";
static void Main(string[] args)
{
writer = new StreamWriter("c:\\StudentsList.txt", true);
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface nic in nics)
{
if (
(nic.NetworkInterfaceType != NetworkInterfaceType.Loopback && nic.NetworkInterfaceType != NetworkInterfaceType.Tunnel) &&
nic.OperationalStatus == OperationalStatus.Up)
{
networkIsAvailable = true;
}
}
if (!networkIsAvailable)
Console.WriteLine("Internet connection not available! We resume as soon as network is available...");
else
ConnectToPUServer();
}
public static void ConnectToPUServer()
{
var client = new WebClient();
while (i < 500 && networkIsAvailable)
{
string html = client.DownloadString(URI);
//some data processing
Console.WriteLine(i);
i++;
URI = "http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/" + i + "/";
}
}
static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
networkIsAvailable = e.IsAvailable;
if (!networkIsAvailable)
Console.WriteLine("Internet connection not available! We resume as soon as network is available...");
while (!networkIsAvailable)
{
//waiting for internet connection
}
ConnectToPUServer();
Console.WriteLine("Complete.");
writer.Close();
}
}
After resuming, why while loop of ConnectToPUServer
is executing only once??
Thanks.
Upvotes: 1
Views: 2237
Reputation: 10862
If NetworkChange_NetworkAvailabilityChanged is being fired every time a NIC changes it status there is no need for the while loop to wait for a connection
I also move the writer.close and the end of the method ConnectToPUServer
public static void ConnectToPUServer()
{
var client = new WebClient();
while (i < 500 && networkIsAvailable)
{
string html = client.DownloadString(URI);
//some data processing
Console.WriteLine(i);
i++;
URI = "http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/" + i + "/";
}
Console.WriteLine("Complete.");
writer.Close();
}
static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
networkIsAvailable = e.IsAvailable;
if (!networkIsAvailable)
{
Console.WriteLine("Internet connection not available! We resume as soon as network is available...");
}
else
{
ConnectToPUServer();
}
}
Upvotes: 2