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);
}
}
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.
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.