Update progress bar on another form
-
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
} -
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
}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
-
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
}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
-
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
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.
-
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.
: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) -
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.
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
-
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
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?!