c# - ShowDialogue not generating DialogueResult.OK -
i'm writing program that's supposed download off github. has link raw file on github. i'm using downloaddataasync
download it, , have progress bar track how far in download. gets 100%, nothing.
i've been following tutorial c# updater bettercoder (the beginning starts here , relevant part part 9 of series).
this part stops working properly:
private void downloadupdate(saveyourupdatexml update) { sharpupdatedownloadform form = new sharpupdatedownloadform(update.uri, update.md5, this.applicationinfo.applicationicon); debug.writeline("form created"); dialogresult result = form.showdialog(this.applicationinfo.context); debug.writeline("got result"); if (result == dialogresult.ok) { string currentpath = this.applicationinfo.applicationassembly.location; string newpath = path.getdirectoryname(currentpath) + "\\" + update.filename; updateapplication(form.tempfilepath, currentpath, newpath, update.launchargs); application.exit(); } }
it never gets "got result" part unless cancel it. also, this.applicationinfo.context returns form. however, "form created".
i think there's wrong way showdialog used or something, i'm not sure what.
edit: happens when sharpupdatedownloadform created.
internal sharpupdatedownloadform(uri location, string md5, icon programicon) { initializecomponent(); if (programicon != null) { this.icon = programicon; } tempfile = path.gettempfilename(); this.md5 = md5; webclient = new webclient(); webclient.downloadprogresschanged += new downloadprogresschangedeventhandler(webclient_downloadprogresschanged); webclient.downloadfilecompleted += new asynccompletedeventhandler(webclient_downloadfilecompleted); bgworker = new backgroundworker(); bgworker.dowork += new doworkeventhandler(bgworker_dowork); bgworker.runworkercompleted += new runworkercompletedeventhandler(bgworker_runworkercompleted); try { webclient.downloaddataasync(location, this.tempfile); } catch { this.dialogresult = dialogresult.no; this.close(); } }
this should happen when download completed:
private void webclient_downloadfilecompleted(object sender, asynccompletedeventargs e) { if (e.error != null) { this.dialogresult = dialogresult.no; this.close(); } else if (e.cancelled) { this.dialogresult = dialogresult.abort; this.close(); } else { lblprogress.text = "verifying download..."; progressbar.style = progressbarstyle.marquee; bgworker.runworkerasync(new string[] {this.tempfile, this.md5}); } }
this bgworker_runworkercompleted
private void bgworker_runworkercompleted(object sender, runworkercompletedeventargs e) { this.dialogresult = (dialogresult)e.result; this.close(); }
and bgworker_dowork
private void bgworker_dowork(object sender, doworkeventargs e) { string file = ((string[])e.argument)[0]; string updatemd5 = ((string[])e.argument)[1]; if (hasher.hashfile(file, hashtype.md5) != updatemd5) e.result = dialogresult.no; else e.result = dialogresult.ok; }
when webclient.downloaddataasync
completes, fires downloaddatacompleted
event, not downloadfilecompleted
- 1 registered.
the fix is, if use webclient.downloaddataasync
, register downloaddatacompleted
event; note 2nd argument webclient_downloaddatacompleted
different.
webclient.downloaddatacompleted += new downloaddatacompletedeventhandler(webclient_downloaddatacompleted);
...
private void webclient_downloaddatacompleted(object sender, downloaddatacompletedeventargs e) { ... }
Comments
Post a Comment