c# - Run simultaneous tasks after a BackgroundWorker has completed -
i'm using backgroundworker
stream text file in backgorund.
when completed want run 2 simultaneous tasks
involve 2 methods heavy calculations. these methods used streamed data calculations.
somehow i've been stuck. first part executed , i'm able stream data. second part involving calculations doesn't execute.
private readonly backgroundworker _streamworker = new backgroundworker(); public mainwindow() { initializecomponent(); _streamworker.workerreportsprogress = true; _streamworker.workersupportscancellation = true; _streamworker.dowork += streamdata; _streamworker.runworkercompleted += worker_runworkercompleted; _streamworker.runworkerasync(); } private void streamdata(object sender, doworkeventargs e) { // stream text file here } private void worker_runworkercompleted(object sender, runworkercompletedeventargs e) { var task1 = task.factory.startnew(heavymethod1); var task2 = task.factory.startnew(heavymethod2); task.waitall(); // after calculations have ended // calculate results here } private void heavymethod1() { // heavy calculations } private void heavymethod2() { // heavy calculations not // related ones in heavymethod1 }
update: 1 very odd thing have noticed if put messagebox.show("");
before calculations works fine!
private void worker_runworkercompleted(object sender, runworkercompletedeventargs e) { var task1 = task.factory.startnew(heavymethod1); var task2 = task.factory.startnew(heavymethod2); task.waitall(); messagebox.show(""); // after calculations have ended // calculate results here }
you don't wait anything. use task1
, task2
task.waitall(new task[]{task1, task2});
update
with messagebox, give chance tasks complete works.
Comments
Post a Comment