c# - How to perform background task without blocking the GUI but transfer back information to main thread? -
i following:
- have button , table on gui.
- when press button, task started
- this task while loop, giving me data on each iteration
how can run loop , obtain data each iteration of in main gui table, without blocking gui? important, because while stop condition again button on gui.
i have tried using backgroundworker
, cannot figure out how send data @ every loop iteration (???) can result @ end, not target. if launch worker in loop (but not have loop in worker), not work.
private void continuouscoordinateaquisition(object sender, doworkeventargs e) { while (continuouspositionaquisitionflag == true) // while monitoring not stopped, positions { // xyzwpr world coordinates robotcoordinatesxyzwprworld xyzwprworld = robi.getrobotposition_xyzwpr_world(); something........... retuns values need in gui // sleep defined time system.threading.thread.sleep(1000); // wait } }
the calling be
backgroundworker bw = new backgroundworker(); bw.dowork += new doworkeventhandler(continuouscoordinateaquisition); bw.runworkercompleted += new runworkercompletedeventhandler(continuouscoordinateaquisitioncompleted);
continuouspositionaquisitionflag
set button (stop button). continuouscoordinateaquisitioncompleted
here done once unfortunately, not every iteration.
you're on right track. should use background worker, don't wait runworkercompletedeventhandler, happens when done.
instead, inside loop periodically call reportprogress method on background worker. trigger progresschanged event can handle in gui thread.
Comments
Post a Comment