Help with ProgressBar
-
Hi I'm having a bit of an issue with my progress bar in WPF. Let me first explain why I want a progress bar. I have a list of products that I save back to a database when clicking a Save button. Sometimes this list can contain quite a lot of items, and this takes a while to save. I use a for loop to loop through and save each item in this list. So what I want to do is increment the progress bar after each loop iteration. Is this possible? Below is what I tried doing but it only updates the progress bar after the loop has finished, even though I'm running it on a different thread:
for (int i = 0; i < someList.Count; i++) { //CODE SAVE LIST ITEM //CODE SAVE LIST ITEM new Thread(delegate() { UpdateProgress(100 / productPartsList.Count); }).Start(); }//end of for loop //UPDATE PROGRESS BAR METHOD public void UpdateProgress(double increment) { progressIncrement = progressIncrement + increment; Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { progressBar1.SetValue(System.Windows.Controls.ProgressBar.ValueProperty, progressIncrement); }, null); }
Any hints? -
Hi I'm having a bit of an issue with my progress bar in WPF. Let me first explain why I want a progress bar. I have a list of products that I save back to a database when clicking a Save button. Sometimes this list can contain quite a lot of items, and this takes a while to save. I use a for loop to loop through and save each item in this list. So what I want to do is increment the progress bar after each loop iteration. Is this possible? Below is what I tried doing but it only updates the progress bar after the loop has finished, even though I'm running it on a different thread:
for (int i = 0; i < someList.Count; i++) { //CODE SAVE LIST ITEM //CODE SAVE LIST ITEM new Thread(delegate() { UpdateProgress(100 / productPartsList.Count); }).Start(); }//end of for loop //UPDATE PROGRESS BAR METHOD public void UpdateProgress(double increment) { progressIncrement = progressIncrement + increment; Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { progressBar1.SetValue(System.Windows.Controls.ProgressBar.ValueProperty, progressIncrement); }, null); }
Any hints?Hi, I think you have to use another DispatcherPriority flag. Because : DispatcherPriority.Background means the enumeration value is 4. Operations are processed after all other non-idle operations are completed. So i would recommend you to use at least DispatcherPriority.Normal or you also could try DispatcherPriority.Render . Cheers Noodles
-
Hi, I think you have to use another DispatcherPriority flag. Because : DispatcherPriority.Background means the enumeration value is 4. Operations are processed after all other non-idle operations are completed. So i would recommend you to use at least DispatcherPriority.Normal or you also could try DispatcherPriority.Render . Cheers Noodles
Thanks Noodles. That didn't help though, and I tried every priority available. Here's another code sample I tried: NOTE: This is called inside my loop, so it gets done for every loop iteration
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(
delegate()
{
System.Windows.Threading.DispatcherOperation
dispatcherOp = prgbSave.Dispatcher.BeginInvoke(
System.Windows.Threading.DispatcherPriority.Render,
new Action(
delegate()
{
progressIncrement = progressIncrement + (100.0 / productPartsList.Count);
prgbSave.Value = progressIncrement;
}
));dispatcherOp.Completed += new EventHandler(dispatcherOp\_Completed); } )); thread.Start();
This does the same :mad: The progressbar only gets updated after the loop has completed.
-
Hi I'm having a bit of an issue with my progress bar in WPF. Let me first explain why I want a progress bar. I have a list of products that I save back to a database when clicking a Save button. Sometimes this list can contain quite a lot of items, and this takes a while to save. I use a for loop to loop through and save each item in this list. So what I want to do is increment the progress bar after each loop iteration. Is this possible? Below is what I tried doing but it only updates the progress bar after the loop has finished, even though I'm running it on a different thread:
for (int i = 0; i < someList.Count; i++) { //CODE SAVE LIST ITEM //CODE SAVE LIST ITEM new Thread(delegate() { UpdateProgress(100 / productPartsList.Count); }).Start(); }//end of for loop //UPDATE PROGRESS BAR METHOD public void UpdateProgress(double increment) { progressIncrement = progressIncrement + increment; Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { progressBar1.SetValue(System.Windows.Controls.ProgressBar.ValueProperty, progressIncrement); }, null); }
Any hints? -
Hi I'm having a bit of an issue with my progress bar in WPF. Let me first explain why I want a progress bar. I have a list of products that I save back to a database when clicking a Save button. Sometimes this list can contain quite a lot of items, and this takes a while to save. I use a for loop to loop through and save each item in this list. So what I want to do is increment the progress bar after each loop iteration. Is this possible? Below is what I tried doing but it only updates the progress bar after the loop has finished, even though I'm running it on a different thread:
for (int i = 0; i < someList.Count; i++) { //CODE SAVE LIST ITEM //CODE SAVE LIST ITEM new Thread(delegate() { UpdateProgress(100 / productPartsList.Count); }).Start(); }//end of for loop //UPDATE PROGRESS BAR METHOD public void UpdateProgress(double increment) { progressIncrement = progressIncrement + increment; Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { progressBar1.SetValue(System.Windows.Controls.ProgressBar.ValueProperty, progressIncrement); }, null); }
Any hints?The normal way to do this in a WPF application is to implement a background worker, where you'd save the items in your background thread, and you'd dispatch the updated progress values back to the calling thread which would update the progress bar.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.