c# - How to use BackgroundWorker with custom Progress Dialog (Form with ProgressBar)? -
i've got method loads files. takes lot of time show user progressbar window, can minimize whole application or cancel progress.
i've read bunch of tutorials backgroundworkers , progressbars none of these covers scenario, when progressbar @ other form. don't know when should open new form , how access progress bar there.
so far i've tried:
mainform:
private void addfoldertoolstripmenuitem_click(object sender, eventargs e) { ctlfolderselectdialog dlg = new ctlfolderselectdialog(); if (dlg.showdialog() == dialogresult.ok) { backgroundworker.runworkerasync(dlg); // pass variables ctlfolderselectdialog } } private void backgroundworker_dowork(object sender, doworkeventargs e) { form_dialogs.progressdialog progressdialog = new form_dialogs.progressdialog(); progressdialog.showdialog(); // open progress dialog window myclass.loadfiles((ctlfolderselectdialog)e.argument); // work progressdialog.close(); // close progress window - in here? }
myclass:
public void loadfiles(ctlfolderselectdialog dlg) { ... files.count() // somehow report preliminary number of files progressbar window - set progressbar1.maximum value ... foreach (file in files) { loadfile(); ... // here should somehow udpate progressbar on other window - performstep() } }
progressdialog:
private void progressdialog_load(object sender, eventargs e) { progressbar1.value = 0; progressbar1.maximum = 100; // how value myclass function? pass in progressdialog contructor? progressbar1.step = 1; } public void updateprogress(int progress) { progressbar1.performstep(); }
backgroundworker
supports progress updates through progresschanged
event.
inside loadfiles()
method need calculate progress in % , inside foreach
loop call backgroundworker.reportprogress(percentage)
. raise progresschanged
event can in event arguments e.progresspercentage
. [you need reference backgroundworker
inside loadfiles()
method]
in progressdialog
form need register event. can pass backgroundworker
in constructor e.g.
public void progressdialog(backgroundworker worker) { worker.progresschanged += progressdialog_progresschanged; } private void progressdialog_progresschanged(object sender, progresschangedeventargs e) { updateprogressbar(e.progresspercentage); }
and in main form:
private void addfoldertoolstripmenuitem_click(object sender, eventargs e) { ctlfolderselectdialog dlg = new ctlfolderselectdialog(); if (dlg.showdialog() == dialogresult.ok) { backgroundworker.runworkerasync(dlg); // pass variables fromctlfolderselectdialog form_dialogs.progressdialog progressdialog = new form_dialogs.progressdialog(backgroundworker); // pass backgroundworker instance progressdialog.showdialog(); // open progress dialog window // no need explicitly close it. dialog form destroyed when closed user. } } private void backgroundworker_dowork(object sender, doworkeventargs e) { myclass.loadfiles((ctlfolderselectdialog)e.argument); // work }
Comments
Post a Comment