c# - Event to find when internet is lost -


i creating windows application using c#. used check whether updated version of product available or not. if yes, user can download using application's ui without opening browser.

one window in application displays status of download using progressbar control. problem is, in case internet gets disconnected, application not come know. say, after 45% of download, network disconnects; progressbar keeps on displaying 45%.

is there property/event raised once such situation occurs? please help. attaching code 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);     } } 

  1. you can make use of networkchange.networkavailabilitychanged event http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.networkavailabilitychanged.aspx tell in case of problem in lan, ex:network cable unplugged or user disables networkinterface.

  2. in case of internet drop need have ping kind of mechanism own server check whether server reachable or not, start timer when starting download ping , check periodically till download completed, once download completed or user cancelled can stop timer.


Comments