c# - Refresh progress bar -


i want refresh progess bar in c# wpf.

this question sounds easy , should able google it. solutions have seen unsatisfy me.

assuming have run long algorithm e.g. 5 different steps. don't know how long take calculate different steps. know have programmed use profiler check how time cpu uses each step (in % of total time steps).

this times e.g.:

method1() takes 3s method2() takes 5s method3() takes 1s 

this approaches:

the "easy" approach:

progressbar pb = new progressbar()                      {                          // total duration of methods                          maximum = 9                      };  method1(); // + 3 3 seconds pb.value += timeformethod1;  method2(); // + 5 5 seconds pb.value += timeformethod2;  method3(); // + 1 1 second pb.value += timeformethod3; 

this pretty easy. has problem. blocks ui thread 9 seconds wich horrible (because user think programm has crashed).

so seems obvious use thread..


the "thread" approach:
has problem need dispach every operation on progressbar slow (for updates on progressbar)

i have written "taskqueue" that. can save work want in queue , thread working these task after run called , updates progressbar (and label) between tasks.

i don't want post code of threadqueue, because , maybe not implemented (yet).

this important part of thread method:

foreach (var threadqueuenode in threadqueue) {     // changes e.g. label displaying thread doing next     threadqueuenode.premainfunction.invoke();      // executes main task     this.result = threadqueuenode.mainfunction.invoke();      // updates progressbar after work done.     threadqueuenode.postmainfunction.invoke(); } 

postmainfunction delegate , e.g. this:

postmainfunction = (action<int>)((value) => this.dispatcher.invoke(() => this.progressbarstatus.value += value)); 

wich professional way update progessbar problem mine?
i'd happy discussion.

thanks , time!

backgroundworker clean , legible:

var bw = new backgroundworker();  bw.dowork += (s, e) => {     var worker = s backgroundworker;     method1();     worker.reportprogress(30);     method2();     worker.reportprogress(80);     method3();     worker.reportprogress(100); };  bw.progresschanged += (s, e) => {     pb.value += e.progresspercentage; };  bw.runworkercompleted += (s, e) =>  {  };  bw.runworkerasync(); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -