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. Update progress bar on another form

Update progress bar on another form

Scheduled Pinned Locked Moved C#
questionannouncement
7 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 have an application that allows users to upload documents. When the user clicks upload, I want to be able to pop up a different form with a progressbar and a label. The idea is for the progress bar to indicate the total progress of the documents being uploaded, and for the label to display the kilobytes that have been uploaded so far. How can I pass the increment percentage through to this form and update the progressbar on a different thread, without the interface stalling and only updating once the operation has finished. I`m using a delegate to pass info though, and I have tried using the backgroundworker, but I don't think that would work for me. Below is a sample of what I am trying to achieve:

    foreach (Document doc in Documents)
    {
    //upload doc
    //show progress on a different form
    }

    M D 2 Replies Last reply
    0
    • E Etienne_123

      Hi I have an application that allows users to upload documents. When the user clicks upload, I want to be able to pop up a different form with a progressbar and a label. The idea is for the progress bar to indicate the total progress of the documents being uploaded, and for the label to display the kilobytes that have been uploaded so far. How can I pass the increment percentage through to this form and update the progressbar on a different thread, without the interface stalling and only updating once the operation has finished. I`m using a delegate to pass info though, and I have tried using the backgroundworker, but I don't think that would work for me. Below is a sample of what I am trying to achieve:

      foreach (Document doc in Documents)
      {
      //upload doc
      //show progress on a different form
      }

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      Assuming your upload work is done on a BackgroundWorker, then use the ProgressChanged event of the background worker, this should update the values in the other form via a function such as... FORM 1

      Form2 form2 = new Form2();
      BackgroundWorker bw = new BackgroundWorker();

      void OnFormLoad()
      {
      bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
      }

      void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
      form2.UpdateProgress(e.ProgressPercentage, e.UserState);
      }

      void StartUpload()
      {
      bw.RunWorkerAsync();
      form2.Show();
      }

      FORM 2

      public void UpdateProgress(int percentageComplete, object otherData)
      {
      progressBar1.Value = percentageComplete;
      Label1.Text = (string)otherData;//depending on what you pass as other data (could be string of file size uploaded
      }

      ..hope this helps

      Don't vote my posts down just because you don't understand them - if you lack the superior intelligence that I possess then simply walk away

      E 1 Reply Last reply
      0
      • E Etienne_123

        Hi I have an application that allows users to upload documents. When the user clicks upload, I want to be able to pop up a different form with a progressbar and a label. The idea is for the progress bar to indicate the total progress of the documents being uploaded, and for the label to display the kilobytes that have been uploaded so far. How can I pass the increment percentage through to this form and update the progressbar on a different thread, without the interface stalling and only updating once the operation has finished. I`m using a delegate to pass info though, and I have tried using the backgroundworker, but I don't think that would work for me. Below is a sample of what I am trying to achieve:

        foreach (Document doc in Documents)
        {
        //upload doc
        //show progress on a different form
        }

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        This can be done by creating custom events on your form that has the ProgressBar and wraps a BackgroundWorker. I have my own custom class that I use for this which I am intending to post in an article real soon but here is the code if it helps - it is using a custom progress bar that has a Marquee property rather than Style, but the calls to this can easily be altered to use the standard one. The worker part of this code is essentially the same as the .NET BackgroundWorker and I have extended the ProgressChangedEventArgs so I can pass two lots of text in addition to the usual.

        using System;
        using System.ComponentModel;
        using System.Drawing;
        using System.Threading;
        using System.Windows.Forms;

        namespace DaveyM69.Windows.Forms
        {
        [ToolboxBitmap(typeof(Form))]
        public class ProgressForm : Form
        {
        public const string NoMessageChange = null;
        public const int NoProgressChange = -1;

            public event DoWorkEventHandler DoWork;
            public event ProgressChangedEventHandler ProgressChanged;
            public event RunWorkerCompletedEventHandler RunWorkerCompleted;
        
            private object argument;
            private AsyncOperation asyncOperation;
            private Button button;
            private bool cancellationPending;
            private readonly SendOrPostCallback completed;
            private bool isBusy;
            private Label label;
            private readonly SendOrPostCallback progress;
            private ProgressBar progressBar;
            private readonly ParameterizedThreadStart work;
            private bool workerReportsProgress;
            private bool workerSupportsCancellation;
        
            public ProgressForm()
            {
                InitializeComponent();
                argument = null;
                asyncOperation = null;
                completed = new SendOrPostCallback(Completed);
                isBusy = false;
                progress = new SendOrPostCallback(Progress);
                work = new ParameterizedThreadStart(Work);
                workerReportsProgress = true;
                workerSupportsCancellation = true;
            }
        
            \[Browsable(false)\]
            public object Argument
            {
                get { return argument; }
                set
                {
                    if (argument != value)
                    {
                        if (!isBusy)
                            argument = value;
                    }
                }
            }
            \[Browsable(false)\]
            public bool CancellationPending
            {
                get { return cancellationPen
        
        L 1 Reply Last reply
        0
        • D DaveyM69

          This can be done by creating custom events on your form that has the ProgressBar and wraps a BackgroundWorker. I have my own custom class that I use for this which I am intending to post in an article real soon but here is the code if it helps - it is using a custom progress bar that has a Marquee property rather than Style, but the calls to this can easily be altered to use the standard one. The worker part of this code is essentially the same as the .NET BackgroundWorker and I have extended the ProgressChangedEventArgs so I can pass two lots of text in addition to the usual.

          using System;
          using System.ComponentModel;
          using System.Drawing;
          using System.Threading;
          using System.Windows.Forms;

          namespace DaveyM69.Windows.Forms
          {
          [ToolboxBitmap(typeof(Form))]
          public class ProgressForm : Form
          {
          public const string NoMessageChange = null;
          public const int NoProgressChange = -1;

              public event DoWorkEventHandler DoWork;
              public event ProgressChangedEventHandler ProgressChanged;
              public event RunWorkerCompletedEventHandler RunWorkerCompleted;
          
              private object argument;
              private AsyncOperation asyncOperation;
              private Button button;
              private bool cancellationPending;
              private readonly SendOrPostCallback completed;
              private bool isBusy;
              private Label label;
              private readonly SendOrPostCallback progress;
              private ProgressBar progressBar;
              private readonly ParameterizedThreadStart work;
              private bool workerReportsProgress;
              private bool workerSupportsCancellation;
          
              public ProgressForm()
              {
                  InitializeComponent();
                  argument = null;
                  asyncOperation = null;
                  completed = new SendOrPostCallback(Completed);
                  isBusy = false;
                  progress = new SendOrPostCallback(Progress);
                  work = new ParameterizedThreadStart(Work);
                  workerReportsProgress = true;
                  workerSupportsCancellation = true;
              }
          
              \[Browsable(false)\]
              public object Argument
              {
                  get { return argument; }
                  set
                  {
                      if (argument != value)
                      {
                          if (!isBusy)
                              argument = value;
                      }
                  }
              }
              \[Browsable(false)\]
              public bool CancellationPending
              {
                  get { return cancellationPen
          
          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          if only someone would write an article on this interesting subject... :laugh:

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          D E 2 Replies Last reply
          0
          • L Luc Pattyn

            if only someone would write an article on this interesting subject... :laugh:

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            :laugh: I'm going to be concentrating on WP7 dev for the forseeable future so WPF will also be a logical move for desktop I suppose. I have built up quite a library of useful components/controls etc for WinForms over the years that I'm combining into one demo project to share.

            Dave
            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            1 Reply Last reply
            0
            • L Luc Pattyn

              if only someone would write an article on this interesting subject... :laugh:

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

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

              I agree :) Thanks for the post from musefan, it did help me quite a lot. My code seemed to work fine, then I renamed a couple of things and then it didn't update the progressbar correctly anymore, then I undid the changes and it still doesn't work :( I know it did work though so I must just be missing something

              1 Reply Last reply
              0
              • M musefan

                Assuming your upload work is done on a BackgroundWorker, then use the ProgressChanged event of the background worker, this should update the values in the other form via a function such as... FORM 1

                Form2 form2 = new Form2();
                BackgroundWorker bw = new BackgroundWorker();

                void OnFormLoad()
                {
                bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
                }

                void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
                {
                form2.UpdateProgress(e.ProgressPercentage, e.UserState);
                }

                void StartUpload()
                {
                bw.RunWorkerAsync();
                form2.Show();
                }

                FORM 2

                public void UpdateProgress(int percentageComplete, object otherData)
                {
                progressBar1.Value = percentageComplete;
                Label1.Text = (string)otherData;//depending on what you pass as other data (could be string of file size uploaded
                }

                ..hope this helps

                Don't vote my posts down just because you don't understand them - if you lack the superior intelligence that I possess then simply walk away

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

                I've realised that this method won't work due to various other methods that gets called from the long operation, methods that change controls etc. This raises the "control accessed from a different thread..." error. I cannot change these methods becauses it is part of a rather tightly coupled project. There must be a way to simply update the progressbar on the LoadingForm using a thread?!

                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