Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi all,
i am new in wpf, currently i need a progress bar that will get active on button click.
in current scenario, there is some code written on button click event which is taking time for execution. i am thinking to show a progress bar for the time taken on execution.
how can i do it???

What I have tried:

case1:- i tried increasing progress bar value after particular lines of code but the progress bar value is changing after full execution of code on button click, till then its blank even if code is written. as the last line of button click states value as
ProgressBar1.value=100;
its directly showing 100.


case2:-
C++
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));

i even tried above line of code, dint help me
Posted
Updated 20-Oct-20 10:25am
v2
Comments
edmundv 7-Apr-17 17:16pm    
The way i normally approach these issues is to place the long running task on a separate thread and update the UI with the progress value. I normally use an async method with a progress handler to report progress back to the UI/etc.

You can do this a few ways, Dispatcher, Task, Backgroundworker, but following along more with what you were doing, write it this way instead:
ProgressBar1.Dispatcher.Invoke(
    new Action(()=>ProgressBar1.Value=newvalue), 
    DispatcherPriority.Background);

Declare the Action delegate separately if you want, but this is basically what you need to do. The important thing is that an update to the UI needs to be done from a thread separate to the rest of your code. See DispatcherPriority[^]. Based on priority, rendering happens after code execution. So if you're looping through some code and updating the progressbar within the same thread, then the progressbar has to wait for the looped code to finish before it can use the thread to render the update. For the same reasons, any kind of heavy work should be separated from the main UI thread also or you lock out the Input level and the application's window becomes unresponsive until the work is done.

On a side note, priority is also why you would use Background for most UI updates. The next priority up is Input. Any user interaction with the UI or moving the window for example would be Input. Above that is Loaded and if you used that to update the control then you have a similar issue as before where this time your control would be updating and rendering like it should, but you would be locked out of interacting with the UI until it was finished because the Input level is forced to wait.
 
Share this answer
 
this solution doesnt work for me, i have correct values assigned to properties Value and Maximum of progressBar, but stil not working.
 
Share this answer
 
Comments
CHill60 21-Oct-20 8:56am    
The poster of the solution is unlikely to randomly come back to a 3 year old post and see your "solution". If you want to comment on a post then use the "Have a Question or Comment?" link next to it. Don't post comments as solutions - I advise you to remove this before it gets downvoted into oblivion

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900