Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Help with ProgressBar

Help with ProgressBar

Scheduled Pinned Locked Moved WPF
helpcsharpdatabasewpfquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Etienne_123
    wrote on last edited by
    #1

    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?

    S N P 3 Replies Last reply
    0
    • E Etienne_123

      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?

      S Offline
      S Offline
      Sperneder Patrick
      wrote on last edited by
      #2

      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

      E 1 Reply Last reply
      0
      • S Sperneder Patrick

        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

        E Offline
        E Offline
        Etienne_123
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • E Etienne_123

          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?

          N Offline
          N Offline
          Nigel Ferrissey
          wrote on last edited by
          #4

          This[^] blog post will show you how to implement a version of DoEvents(), that should help.

          1 Reply Last reply
          0
          • E Etienne_123

            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?

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            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.

            My blog | My articles | MoXAML PowerToys | Onyx

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups