Threading and PgressBars

G

Guest

Hi,

I have a MainForm Class that loads childforms inside a panel... this main
form has a toolbar.
Currently I use a thread load a splash, that is working currectly, now I
want to have another thread to show the progress in the status bar of some
processes I need to do.

Here is the scene:
public MainForm()
{

InitializeComponent();
......
this.EndSplash();
Thread process = new Thread( new ThreadStart(preprocess));
process.Start();
}

static void Main()
{
Thread th = new Thread(new ThreadStart(DoSplash));
th.Start();
Thread.Sleep(500);
........................
sp.label1.Text= "Ready, Now Loading the program!";
Thread.Sleep(500);
.......................
Application.Run(new MainForm());

public void EndSplash()
{
sp.Invoke(new MethodInvoker(sp.Close));
}
static Splash sp = new Splash();
private static void DoSplash()
{
sp = new Splash();
sp.ShowDialog();
}

public void PreProcessing()
{
ColouredProgressBar pbar = new ColouredProgressBar.ColouredProgressBar();
StatusBarPanel ProgressPanel = new StatusBarPanel();
statusBar1.Parent = this;
statusBar1.ShowPanels = true;
StatusBarPanel panel1 = new StatusBarPanel();
panel1.AutoSize = StatusBarPanelAutoSize.Contents;
panel1.Text = "Mein Text";
statusBar1.Panels.Add(panel1);
ProgressPanel.AutoSize = StatusBarPanelAutoSize.None;
statusBar1.Panels.Add(ProgressPanel );

pbar.Parent = statusBar1;
pbar.Width = ProgressPanel.Width;
pbar.Location = new Point(panel1.Width, 0);
pbar.Height = statusBar1.Height -2;
pbar.Minimum = 0;
.............................
Get Maximum by SQL
............................
pbar.Maximum = Convert.ToInt16(oCmdCount.ExecuteScalar());
pbar.Value = 0;

.............................
Do a loop and each time I process info I increment
the progressbar (pbar) value
..............................

pbar.Value = pbar.Value++;

..............................
Get Out of the loop
...............................

panel1.Text="Process Completed";
}

I didi this way but It doesn't work.... any suggestions or corrections will
be appreciated
 
N

Nicholas Paldino [.NET/C# MVP]

Diogo,

A few things to point out.

Rather than creating a splash screen and having your form close it
(which introduces a tight coupling), you should create your splash page, and
un it in it's own call to Run. The splash screen can trigger off whatever
setup you have to do, then you can make another call to Run to show your
main form, like so:

// In the entry point to your program.
// Run the splash screen.
Application.Run(new SplashScreen());

// Run the main frm.
Application.Run(new MainForm());

That makes things much easier.

As for the call to PreProcessing, you can't make calls to visual
components from a thread other than the thread they were created on. When
you need to make a call to a UI component, create a delegate, and then
create a method which will perform the UI operations (which has the same
signature as the delegate). Then, from the thread that is doing your
processing, make a call to the Invoke method on the control instance that
was created on the UI thread, passing the delegate your created. It will
then call this method on the UI thread, and it should work.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top