c# - Proper way to sync a wpf progress bar with function from a library -
so have function (from library) works big word files. can't make changes on function. while processing, want show progress bar, because either way app looks frozen , users not aware it's working. i'm using worker this:
private void btnclick(object sender, routedeventargs e) { backgroundworker worker = new backgroundworker(); worker.runworkercompleted += worker_runworkercompleted; worker.workerreportsprogress = true; worker.dowork += worker_doconvertone; worker.progresschanged += worker_progresschanged; worker.runworkerasync(); } private void worker_doconvertone(object sender, doworkeventargs e) { var worker = sender backgroundworker; //the progress bar filled on 20% worker.reportprogress(0); worker.reportprogress(10); worker.reportprogress(20); //processing mylonglastingfunction(bigwordfile); //the progress bas full worker.reportprogress(100, "done processing."); } private void worker_runworkercompleted(object sender, runworkercompletedeventargs e) { messagebox.show("converting finished!"); testprogressbar.value = 0; progresstextblock.text = ""; } private void worker_progresschanged(object sender, progresschangedeventargs e) { testprogressbar.value = e.progresspercentage; progresstextblock.text = (string)e.userstate; }
it's doing work, it's workaround , want know if there proper way solve problem. in advance. :)
i take question altering mylonglastingfunction
not possible give periodic updates on progress, , ability not exist in function.
for these scenarios when duration of task cannot determined progress bar dependency property isindeterminate="true"
accepted way give information user. animates progress bar scrolling continuously.
xaml
<progressbar margin="10" isindeterminate="true" />
i prefer animated moving dots seen on windows phone. example has been implemented here.
if not require next best method estimate total time, , subdividing time dispatchtimer
give periodic event increment progress bar. has 2 problems of either finishing before reaching 100% (not bad thing) or reaching 100% , getting stuck there actual time longer estimate. second undesired effect make application inactive again.
Comments
Post a Comment