c# - How to use Task.run to perform the function of Do_Work() of a background worker -
i move 4 objects in parallel in specific paths stored in list of paths, when each of them complete 1 path(specific coordinates) switches another!.
i used 4 background workers perform such job in background , in each call each background worker should try 6 paths (chromosomes) extracted text file , each path stored in different 6 lists , each list contains coordinates each path. coordinates converted 2d points perform projection , each path @ specific depth paths move these objects on different layers using projection technique i.e. ach object (worker) moved @ different layer.
each worker should move object forward , backward using 1 path (chromosome) switches next , should finish first attempt (path) before switch next in order calculate time consumed , other factors such "fitness" function.
the following example on 1 do_work() methods:
private void auv0genetic_dowork(object sender, doworkeventargs e) { list<pointf> genetic2dlayerpath1 = new list<pointf>(); // chromosome 1 list<pointf> genetic2dlayerpath2 = new list<pointf>(); // chromosome 2 list<pointf> genetic2dlayerpath3 = new list<pointf>(); // chromosome 3 list<pointf> genetic2dlayerpath4 = new list<pointf>(); // chromosome 4 list<pointf> genetic2dlayerpath5 = new list<pointf>(); // chromosome 5 list<pointf> genetic2dlayerpath6 = new list<pointf>(); // chromosome 6 countnumofpaths_auv_1 = 0; float[] xpoints = new float[1]; float[] ypoints = new float[1]; foreach (int[,] arr in pathchromosom1) { point3d pointin3d = new point3d(cellscenters[0, arr[0, 0]], cellscenters[1, arr[1, 0]], 700); pointf pointin2d = project(pointin3d); // convert 2d genetic2dlayerpath1.add(pointin2d); } foreach (int[,] arr in pathchromosom2) { point3d pointin3d = new point3d(cellscenters[0, arr[0, 0]], cellscenters[1, arr[1, 0]], 700); pointf pointin2d = project(pointin3d); // convert 2d genetic2dlayerpath2.add(pointin2d); } foreach (int[,] arr in pathchromosom3) { point3d pointin3d = new point3d(cellscenters[0, arr[0, 0]], cellscenters[1, arr[1, 0]], 700); pointf pointin2d = project(pointin3d); // convert 2d genetic2dlayerpath3.add(pointin2d); } foreach (int[,] arr in pathchromosom4) { point3d pointin3d = new point3d(cellscenters[0, arr[0, 0]], cellscenters[1, arr[1, 0]], 700); pointf pointin2d = project(pointin3d); // convert 2d genetic2dlayerpath4.add(pointin2d); } foreach (int[,] arr in pathchromosom5) { point3d pointin3d = new point3d(cellscenters[0, arr[0, 0]], cellscenters[1, arr[1, 0]], 700); pointf pointin2d = project(pointin3d); // convert 2d genetic2dlayerpath5.add(pointin2d); } foreach (int[,] arr in pathchromosom6) { point3d pointin3d = new point3d(cellscenters[0, arr[0, 0]], cellscenters[1, arr[1, 0]], 700); pointf pointin2d = project(pointin3d); // convert 2d genetic2dlayerpath6.add(pointin2d); } int counter = 0; (int = 0; < 6; i++) { if (i == 0) // first chromosome { xpoints = new float[genetic2dlayerpath1.count()]; ypoints = new float[genetic2dlayerpath1.count()]; auv[0].auvdepth = 700; foreach(pointf p in genetic2dlayerpath1) { xpoints[counter] = p.x; ypoints[counter] = p.y; counter++; } counter = 0; } if (i == 1) // second chromosome { xpoints = new float[genetic2dlayerpath2.count()]; ypoints = new float[genetic2dlayerpath2.count()]; auv[0].auvdepth = 700; foreach (pointf p in genetic2dlayerpath2) { xpoints[counter] = p.x; ypoints[counter] = p.y; counter++; } counter = 0; } if (i == 2) // third chromosome { xpoints = new float[genetic2dlayerpath3.count()]; ypoints = new float[genetic2dlayerpath3.count()]; auv[0].auvdepth = 700; foreach (pointf p in genetic2dlayerpath3) { xpoints[counter] = p.x; ypoints[counter] = p.y; counter++; } counter = 0; } if (i == 3) // fourth chromosome { xpoints = new float[genetic2dlayerpath4.count()]; ypoints = new float[genetic2dlayerpath4.count()]; auv[0].auvdepth = 700; foreach (pointf p in genetic2dlayerpath4) { xpoints[counter] = p.x; ypoints[counter] = p.y; counter++; } counter = 0; } if (i == 4) // fifth chromosome { xpoints = new float[genetic2dlayerpath5.count()]; ypoints = new float[genetic2dlayerpath5.count()]; auv[0].auvdepth = 700; foreach (pointf p in genetic2dlayerpath5) { xpoints[counter] = p.x; ypoints[counter] = p.y; counter++; } counter = 0; } if (i == 5) // sixth chromosome { xpoints = new float[genetic2dlayerpath6.count()]; ypoints = new float[genetic2dlayerpath6.count()]; auv[0].auvdepth = 700; foreach (pointf p in genetic2dlayerpath6) { xpoints[counter] = p.x; ypoints[counter] = p.y; counter++; } counter = 0; } counter = 0; while (countnumofpaths_auv_1 != 2) { thread.sleep(900); // assume represents speed of auv in our case = 3 m/s each meter equal 300 seconds in thread.sleep() if (auv0genetic.cancellationpending) { e.cancel = true; return; } if (forward) { if (counter == xpoints.length - 1) { backward = true; forward = false; countnumofpaths_auv_1++; } else { auv[0].auvx = xpoints[counter]; auv[0].auvy = ypoints[counter]; counter++; } } if (backward) { if (counter == 0) { backward = false; forward = true; countnumofpaths_auv_1++; } else { auv[0].auvx = xpoints[counter]; auv[0].auvy = ypoints[counter]; counter--; } } //////////////////////// draw /////////////////////////// isetupdisplay = 0; if (isetupdisplay != -1) { isetupdisplay += 10; if (isetupdisplay >= topology.width) isetupdisplay = -1; topology.refresh(); } ///////////////////////////////////////////////////////// } } } i declared each background worker this:
auv0genetic = new backgroundworker(); auv0genetic.workersupportscancellation = true; auv0genetic.dowork += new doworkeventhandler(auv0genetic_dowork); auv0genetic.runworkercompleted += new runworkercompletedeventhandler(auv0genetic_runworkercompleted); i declared them in loop loops 250 times , call them inside loop each time calling method contains following lines:
auv0genetic.runworkerasync(geneticiteration); // start auv # 1 problems :
there no synchronization between loop , do_work() method i.e. loop starts new iteration before 4 backgrounds worker finishes work each iteration there list contains 6 different paths (chromosomes) , each background worker should tries them before next iteration new list. need stop workers before going next iteration. put message box outside loop , not appeared after completion of loop after workers stop.
my question is:
i faced problems using background workers wondered if possible use task class instead, if .. how use task.run perform same job inside do_work() methods ?
the main concept is:
public async task workerstartedmathod() { for(int = 0; i<=250; i++) { list<task> tasks = new list<task>(); tasks.add(taks.run(auv0genetic_dowork)); tasks.add(taks.run(auv0genetic_dowork)); tasks.add(taks.run(auv0genetic_dowork)); tasks.add(taks.run(auv0genetic_dowork)); tasks.add(taks.run(auv0genetic_dowork)); tasks.add(taks.run(auv0genetic_dowork)); await task.whenall(tasks); } } however have asumption "dowork" method should rewrited in case need access ui thread. should add dispatchers in parts try change simething on ui.
Comments
Post a Comment