c# - Making a progress bar update in real time in wpf -


i'm having trouble making progress bar show updates in real time.

this code right now

for (int = 0; < 100; i++) {      progressbar1.value = i;      thread.sleep(100); } 

but reason progress bar shows empty when function runs, , nothing until function finishes running. can explain me how can done? i'm new c#/wpf i'm not 100% sure on how implement dispatcher on different thread (as seen on other posts) fix problem.

to clarify, program has button when press, grabs value textbox, , uses api retrieve info, , create labels based on it. want progress bar update after every row of data finished processing.

this have right now:

private async void search(object sender, routedeventargs e) {     var progress = new progress<int>(value => progressbar1.value = value);     await task.run(() =>     {         this.dispatcher.invoke((action)(() =>         {              pre-processing before actual loop occur              (int = 0; < numberofrows; i++)              {                   label creation + adding                   ((iprogress<int>)progress).report(i);              }         }));     }); } 

thank you!

if using .net 4.5 or later, can use async/await:

var progress = new progress<int>(value => progressbar.value = value); await task.run(() => {     (int = 0; < 100; i++)     {         ((iprogress<int>)progress).report(i);         thread.sleep(100);     } }); 

you need mark method async keyword able use await, example:

private async void button_click(object sender, routedeventargs e) 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -