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. C#
  4. Thread Progress Bar

Thread Progress Bar

Scheduled Pinned Locked Moved C#
csharpwpf
6 Posts 5 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.
  • J Offline
    J Offline
    juliogyn
    wrote on last edited by
    #1

    I have this code:

    public void mtdProgressbar() {

            prgsBar.IsIndeterminate = false;
    
            prgsBar.Orientation = Orientation.Horizontal;
    
            prgsBar.Minimum = 0;
    
            prgsBar.Maximum = short.MaxValue;
    
            prgsBar.Value = 0;
    
            double value = 0;
    
            prgsBar.Visibility = Visibility;
    
            UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(prgsBar.SetValue);
    
            do
            {
                value += 1;
    
                Dispatcher.Invoke(updatePbDelegate,   System.Windows.Threading.DispatcherPriority.Background,new object\[\] { ProgressBar.ValueProperty, value });
            }
            while (prgsBar.Value != prgsBar.Maximum);
    
            prgsBar.Visibility = Visibility.Hidden;
        }
    

    But i can not create the Thread do fill progressbar, do not work. The code:

    .
    .
    .
    else{
    System.Threading.Thread trd = new System.Threading.Thread(new System.Threading.ThreadStart(this.mtdProgressbar));
    trd.IsBackground = true;
    trd.Start();
    mtdTest(); //this method contains only loop for,represents an operation that requires the progressbar
    }

    I'm using C# and Wpf

    F D V 3 Replies Last reply
    0
    • J juliogyn

      I have this code:

      public void mtdProgressbar() {

              prgsBar.IsIndeterminate = false;
      
              prgsBar.Orientation = Orientation.Horizontal;
      
              prgsBar.Minimum = 0;
      
              prgsBar.Maximum = short.MaxValue;
      
              prgsBar.Value = 0;
      
              double value = 0;
      
              prgsBar.Visibility = Visibility;
      
              UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(prgsBar.SetValue);
      
              do
              {
                  value += 1;
      
                  Dispatcher.Invoke(updatePbDelegate,   System.Windows.Threading.DispatcherPriority.Background,new object\[\] { ProgressBar.ValueProperty, value });
              }
              while (prgsBar.Value != prgsBar.Maximum);
      
              prgsBar.Visibility = Visibility.Hidden;
          }
      

      But i can not create the Thread do fill progressbar, do not work. The code:

      .
      .
      .
      else{
      System.Threading.Thread trd = new System.Threading.Thread(new System.Threading.ThreadStart(this.mtdProgressbar));
      trd.IsBackground = true;
      trd.Start();
      mtdTest(); //this method contains only loop for,represents an operation that requires the progressbar
      }

      I'm using C# and Wpf

      F Offline
      F Offline
      Forbiddenx
      wrote on last edited by
      #2

      Threads can be tricky, what you are running into is a problem with access.. threads by default can not access the ui to update a progress bar because that may create a deadlock. For the background worker there is a function built in for report progress, but it is limited to only allow for simple updates. You can however, get around this requirement by doing .Invoke((Action)delegate() which allows safe access to the ui through a delegate. I have used this method to update multiple labels, etc. That should do the trick.

      =)

      B 1 Reply Last reply
      0
      • J juliogyn

        I have this code:

        public void mtdProgressbar() {

                prgsBar.IsIndeterminate = false;
        
                prgsBar.Orientation = Orientation.Horizontal;
        
                prgsBar.Minimum = 0;
        
                prgsBar.Maximum = short.MaxValue;
        
                prgsBar.Value = 0;
        
                double value = 0;
        
                prgsBar.Visibility = Visibility;
        
                UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(prgsBar.SetValue);
        
                do
                {
                    value += 1;
        
                    Dispatcher.Invoke(updatePbDelegate,   System.Windows.Threading.DispatcherPriority.Background,new object\[\] { ProgressBar.ValueProperty, value });
                }
                while (prgsBar.Value != prgsBar.Maximum);
        
                prgsBar.Visibility = Visibility.Hidden;
            }
        

        But i can not create the Thread do fill progressbar, do not work. The code:

        .
        .
        .
        else{
        System.Threading.Thread trd = new System.Threading.Thread(new System.Threading.ThreadStart(this.mtdProgressbar));
        trd.IsBackground = true;
        trd.Start();
        mtdTest(); //this method contains only loop for,represents an operation that requires the progressbar
        }

        I'm using C# and Wpf

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        You don't put the progress bar in the background thread. It MUST stay on the UI (startup) thread. Your work goes in the background thread and then Invokes a method that updates the progress bar on the UI thread.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        1 Reply Last reply
        0
        • F Forbiddenx

          Threads can be tricky, what you are running into is a problem with access.. threads by default can not access the ui to update a progress bar because that may create a deadlock. For the background worker there is a function built in for report progress, but it is limited to only allow for simple updates. You can however, get around this requirement by doing .Invoke((Action)delegate() which allows safe access to the ui through a delegate. I have used this method to update multiple labels, etc. That should do the trick.

          =)

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #4

          Forbiddenx wrote:

          You can however, get around this requirement by doing
          .Invoke((Action)delegate() which allows safe access to the ui through a delegate.

          Hi, isn't the OP already doing this in his code:

          do
          {
          value += 1;

          Dispatcher.Invoke(updatePbDelegate,   System.Windows.Threading.DispatcherPriority.Background,new object\[\] { ProgressBar.ValueProperty, value });
          

          }

          ? Of course, the OP didn't disclose the code for 'updatePbDelegate, or show where it's created. In this case I think the problem is elsewhere (see Dave K.'s response).

          Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

          F 1 Reply Last reply
          0
          • B BillWoodruff

            Forbiddenx wrote:

            You can however, get around this requirement by doing
            .Invoke((Action)delegate() which allows safe access to the ui through a delegate.

            Hi, isn't the OP already doing this in his code:

            do
            {
            value += 1;

            Dispatcher.Invoke(updatePbDelegate,   System.Windows.Threading.DispatcherPriority.Background,new object\[\] { ProgressBar.ValueProperty, value });
            

            }

            ? Of course, the OP didn't disclose the code for 'updatePbDelegate, or show where it's created. In this case I think the problem is elsewhere (see Dave K.'s response).

            Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

            F Offline
            F Offline
            Forbiddenx
            wrote on last edited by
            #5

            Yes, I do see invoke but it does not look like its used correctly. It looks to me like his code is trying to access the progress bar ui directly and outside of the invoke. It looks like he is setting a lot of values, my guess is some of those values are on the ui and that is what is crashing everything. I think he is going to have to re-write how this works..

            =)

            1 Reply Last reply
            0
            • J juliogyn

              I have this code:

              public void mtdProgressbar() {

                      prgsBar.IsIndeterminate = false;
              
                      prgsBar.Orientation = Orientation.Horizontal;
              
                      prgsBar.Minimum = 0;
              
                      prgsBar.Maximum = short.MaxValue;
              
                      prgsBar.Value = 0;
              
                      double value = 0;
              
                      prgsBar.Visibility = Visibility;
              
                      UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(prgsBar.SetValue);
              
                      do
                      {
                          value += 1;
              
                          Dispatcher.Invoke(updatePbDelegate,   System.Windows.Threading.DispatcherPriority.Background,new object\[\] { ProgressBar.ValueProperty, value });
                      }
                      while (prgsBar.Value != prgsBar.Maximum);
              
                      prgsBar.Visibility = Visibility.Hidden;
                  }
              

              But i can not create the Thread do fill progressbar, do not work. The code:

              .
              .
              .
              else{
              System.Threading.Thread trd = new System.Threading.Thread(new System.Threading.ThreadStart(this.mtdProgressbar));
              trd.IsBackground = true;
              trd.Start();
              mtdTest(); //this method contains only loop for,represents an operation that requires the progressbar
              }

              I'm using C# and Wpf

              V Offline
              V Offline
              V 0
              wrote on last edited by
              #6

              See here[^] It's from one of the CP members... (btw) hope this helps

              V.
              (MQOTD Rules and previous Solutions )

              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