c# - Wait for DownloadFileAsync to finish downloading and then do something -
so downloadfile is:
public void downloadfile() { settings_btn.enabled = false; label1.text = "checking updates..."; //defines server's update directory string server = "http://downloadurl/update/"; //defines application root string root = appdomain.currentdomain.basedirectory; //make sure version file exists filestream fs = null; if (!file.exists("pversion")) { using (fs = file.create("pversion")){} using (streamwriter sw = new streamwriter("pversion")){sw.write("1.0");} } //checks client version string lclversion; using (streamreader reader = new streamreader("pversion")) { lclversion = reader.readline(); } decimal localversion = decimal.parse(lclversion); //server's list of updates xdocument serverxml = xdocument.load(@server + "pupdates.xml"); //the update process foreach (xelement update in serverxml.descendants("pupdate")) { string version = update.element("pversion").value; string file = update.element("pfile").value; decimal serverversion = decimal.parse(version); string surltoreadfilefrom = server + file; string sfilepathtowritefileto = root + file; if (serverversion > localversion) { using (webclient = new webclient()) { webclient.downloadfilecompleted += new asynccompletedeventhandler(completed); webclient.downloadprogresschanged += new downloadprogresschangedeventhandler(progresschanged); // variable holding url address (making sure starts http://) uri url = new uri(surltoreadfilefrom); // start stopwatch using calculate download speed sw.start(); try { // start downloading file webclient.downloadfileasync(url, sfilepathtowritefileto); // change running executable can overwritten. process thisprocess = process.getcurrentprocess(); string me = thisprocess.mainmodule.filename; string bak = me + ".bak"; if (file.exists(bak)) { file.delete(bak); } file.move(me, bak); file.copy(bak, me); //unzip using (zipfile zip = zipfile.read(file)) { foreach (zipentry zipfiles in zip) { zipfiles.extract(root + "\\", true); } } //download new version file webclient.downloadfile(server + "pversion.txt", @root + "pversion"); //delete zip file deletefile(file); var spawn = process.start(me); thisprocess.closemainwindow(); thisprocess.close(); thisprocess.dispose(); } catch (exception ex) { messagebox.show(ex.message); } } } } }
and problem once finds new version , starts downloading file webclient.downloadfileasync(url, sfilepathtowritefileto);
, instantly runs code below changing name, unzipping , downloading new version file progress
what want wait finish downloading file, , rest. how do this?
-- in case necessary, progresschanged:
private void progresschanged(object sender, downloadprogresschangedeventargs e) { label3.text = string.format("{0} kb/s", (e.bytesreceived / 1024d / sw.elapsed.totalseconds).tostring("0.00")) + " " + string.format("{0} mb's / {1} mb's", (e.bytesreceived / 1024d / 1024d).tostring("0.00"), (e.totalbytestoreceive / 1024d / 1024d).tostring("0.00"));; progressbar1.value = e.progresspercentage; label1.text = e.progresspercentage.tostring() + "%"; }
and completed:
private void completed(object sender, asynccompletedeventargs e) { sw.reset(); if (e.cancelled == true) { label1.text = "download cancelled!"; } else { label1.text = "download completed!"; } }
you can use downloadfile method. async word means method run asynchronous (in other thread), that's why it's goes next line praticaly instantly. if want wait download ends, use downloadfile instead of downloadfileasync
Comments
Post a Comment