I have done this by creating a separate thread from you main frame thread to execute the code that sends out the progress update, where you can call setProgress on your progress bar, but you must create a delegate method otherwise you will get a runtime exception that your thread is accessing a control on the main thread, here is what i would do,
declare a delegate method in your the class were you have the progress bar,
public delegate void SetProgressDelg(int level);
Then implement this method to update your progress bar,
public void SetProgress(int level)
{
if (this.InvokeRequired)
{
SetProgressDelg dlg = new SetProgressDelg(this.SetProgress);
this.Invoke(dlg, level);
return;
}
progressBar.Value = level;
}
hope this works for, I use this in several applications and it works great.
Here is how you build the progress bar,
ToolStripContainer = toolStripContainer1 = new System.Windows.Forms.ToolStripContainer();
// StatusBar
//
ToolStripStatusLabel StatusBar = new System.Windows.Forms.ToolStripStatusLabel();
StatusBar.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
StatusBar.ForeColor = System.Drawing.Color.Blue;
StatusBar.LinkColor = System.Drawing.Color.Navy;
StatusBar.Name = "StatusBar";
StatusBar.Size = new System.Drawing.Size(732, 20);
StatusBar.Spring = true;
StatusBar.Text = "Status Messages Go Here";
StatusBar.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// ProgressBar
//
ToolStripProgressBar ProgressBar = new System.Windows.Forms.ToolStripProgressBar();
ProgressBar.ForeColor = System.Drawing.Color.Yellow;
ProgressBar.Name = "ProgressBar";
ProgressBar.Size = new System.Drawing.Size(150, 19);
//
// StatusStrip
//
StatusStrip StatusStrip = new System.Windows.Forms.StatusStrip();
StatusStrip.Dock = System.Windows.Forms.DockStyle.None;
StatusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
StatusBar, this.ProgressBar});
StatusStrip.Location = new System.Drawing.Point(0, 0);
StatusStrip.Name = "StatusStrip";
StatusStrip.Size = new System.Drawing.Size(899, 25);
StatusStrip.TabIndex = 0;
toolStripContainer1.BottomToolStripPanel.Controls.Add(this.StatusStrip);
then you want to add the toolStripContainer to you controls of the main panel.
you want to call SetProgress from the thread that is processing your task,
here is how you start a thread,
//* from your class of your main frame
//* this is where SetStatus is defined
//* start a thread to process whatever task
//* is being done
Thread t = new Thread(StartProc);
t.Start();
public void StartProc()
{
//* start processing something,
//*let's assume your are processing a bunch of files
List<string> fileNames;
for (int i = 0; i < fileNames.Count; i++)
{
//* process file here
//* ...........
//* then update progress bar
SetProgress((int)((i + 1) * 100 / fileNames.Length));
}
//* thread will exit here
}
Let me know if you need something else, hope this helps,