c# - Background worker runs doWork multiple times -
private void button_uploadtopi_click(object sender, routedeventargs e) { this.newfilepath = this.textbox_input_filepath.text; this.label_status.content = ""; if (bgworker.isbusy != true) { bgworker.runworkerasync(); } }
here click event.
public mainwindow() { initializecomponent(); this.progressbar.minimum = 0; this.progressbar.maximum = 100; this.bgworker = new backgroundworker(); this.bgworker.workerreportsprogress = true; this.bgworker.workersupportscancellation = true; reader.isopen = false; this.bgworker.dowork += bgworker_dowork; this.bgworker.progresschanged += bgworker_progresschanged; this.bgworker.runworkercompleted += bgworker_runworkercompleted; }
main window method.
public partial class mainwindow : window { private backgroundworker bgworker;
bgworker initialized here.
void bgworker_runworkercompleted(object sender, runworkercompletedeventargs e) { this.progressbar.value = 0; this.label_progresspercentage.content = "0%"; reporttextbox.scrolltoend(); }
runworkcomplete code
the main issue is, if run application once, works expected. if click again, execute method twice, , if click again execute method 3 times. increment every new click.
can see main issue here?
a sample worked on against @galenus's suggestion. had same situation background worker, clicking work button firing dowork multiple times until initialized worker button. code block:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes; using system.componentmodel; using system.data; namespace mouseclick { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { public mainwindow() { initializecomponent(); } public backgroundworker worker = new backgroundworker(); private void button_click(object sender, routedeventargs e) { //e.handled = true; // din't worker = new backgroundworker(); //stopped dowork being executed multiple times when button pressed again in same session worker.workerreportsprogress = true; worker.workersupportscancellation = true; worker.dowork += worker_dowork; worker.progresschanged += worker_progresschanged; worker.runworkercompleted += worker_runworkercompleted; worker.runworkerasync(); } void worker_dowork(object sender, doworkeventargs e) { int tot = 1; messagebox.show(tot.tostring()); //if message box appears multiple times, means dowork executing multiple times datatable dt = new datatable(); dt.columns.add("running number"); (int = 0; <= 100; i++) { if (worker.cancellationpending == true) { //http://stackoverflow.com/questions/8300799/cancel-background-worker-exception-in-e-result // e.cancel = true; //this trick e.result = 100; return; } worker.reportprogress(i); system.threading.thread.sleep(1); dt.rows.add(i); } e.result = dt; } #region "worker_progresschanged" void worker_progresschanged(object sender, progresschangedeventargs e) { } #endregion #region "worker_runworkercompleted" void worker_runworkercompleted(object sender, runworkercompletedeventargs e) { mygrid.itemssource = ((datatable)e.result).defaultview; worker.dispose(); } #endregion } }
Comments
Post a Comment