3

I am creating a windows application using C#. It is used to check whether any updated version of a product is available or not. If yes, the user can download it using the application's UI only without opening any browser.

One window in the application displays the status of the download using ProgressBar control. The problem is, in case the internet gets disconnected, the application does not come to know. Say, after 45% of the download, the network disconnects; but the ProgressBar keeps on displaying 45%.

Is there any property/event that is raised once such situation occurs? Please help. Attaching my code as well for your reference. Thanks.

private void CheckForUpdate_Load(object sender, EventArgs e)
{
    string downloadURL = Convert.ToString(ConfigurationManager.AppSettings["TempDownloadURL"]);

    WebClient wcDownloadFile = new WebClient();
    Uri myUri = new Uri(downloadURL);

    wcDownloadFile.DownloadFileAsync(myUri, downloadLocation);
    wcDownloadFile.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wcDownloadFile_DownloadProgressChanged);
}

void wcDownloadFile_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    try
    {
        int bytesDownloaded = Int32.Parse(e.BytesReceived.ToString());
        int totalBytes = Int32.Parse(e.TotalBytesToReceive.ToString());

        progBarSoftPhone.Value = e.ProgressPercentage;
        lblStatus.Text = (bytesDownloaded / 1024).ToString() + " KB out of " + (totalBytes / 1024).ToString() + " KB downloaded (" + e.ProgressPercentage.ToString() + "%).";
    }
    catch (Exception ex)
    {
        MessageBox.Show("ERROR: " + ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
Abhinav
  • 65
  • 1
  • 6
  • How about a timer checking for connection every one minute. Is it really that bad idea? – Sandy Jul 11 '13 at 15:00
  • @Rapsalands - Timer did help. However, I was actually not advised to use Timers until it comes to the last resort. – Abhinav Jul 15 '13 at 11:58

2 Answers2

2
  1. You can make use of NetworkChange.NetworkAvailabilityChanged event http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.networkavailabilitychanged.aspx which will tell you in case of problem in LAN, ex:network cable unplugged or the user itself disables NetworkInterface.

  2. In case of Internet drop you need to have some ping kind of mechanism to your own server to check whether server is reachable or not, you could start a Timer when starting download ping and check periodically till the download completed, once download completed or user cancelled you can stop the timer.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • yes @Sriram, using "NetworkAvailabilityChanged", I could trap the event when my internet was dropping off. – Abhinav Jul 16 '13 at 11:45
1

Add an event listener for DownloadFileCompleted event and check the Error property of AsyncCompletedEventArgs.

In your CheckForUpdate_Load method add:

wcDownloadFile.DownloadFileCompleted += WebClOnDownloadFileCompleted;

And then in the handler you can stop the progress bar in case an error occurred:

private void WebClOnDownloadFileCompleted(object sender, 
    AsyncCompletedEventArgs asyncCompletedEventArgs)
                    {
                       if (asyncCompletedEventArgs.Error != null)
                        // code to handle it
                    }
Adrian Fâciu
  • 12,414
  • 3
  • 53
  • 68
  • Also see this answer for configuring the timeout http://stackoverflow.com/a/3052637/860585 – Rotem Jul 11 '13 at 11:38
  • Thanks for the reply Adrian, but which object's Error property are you referring to? – Abhinav Jul 11 '13 at 11:41
  • @Adrian, thanks for the code. The problem is, in case the internet goes away, the 'completed' event is not called. I am checking this by taking out my LAN cable during download. – Abhinav Jul 11 '13 at 11:54
  • Have a look at the link provided by Rotem to see how you can specify a timeout. Also have a look at this question: http://stackoverflow.com/questions/4745553/detecting-connection-drops-while-webclient-is-downloading-a-file-asynchronously – Adrian Fâciu Jul 11 '13 at 11:57
  • Thanks Adrian & Rotem for all the help. :-) The issue has been sorted out. – Abhinav Jul 15 '13 at 11:56