ProgressBar not updating

  • Thread starter Thread starter martin
  • Start date Start date
M

martin

I'm trying to display a progress bar while some background processing
is done in my PPC app. I am using Control.Invoke, but it's not
working quite right. Here's a code snippet:

private void Start()
{
this.mLoading = true;
Thread loadingThread = new Thread(new ThreadStart(AnimateProgressBar));
loadingThread.Start();

/* some long running processes */

this.mLoading = false;
}

private void AnimateProgressBar()
{
while (this.mLoading)
{
this.mLoadingProgressBar.Invoke(new EventHandler(this.SetProgressBar));
// i've experimented with Thread.Sleep()
// Thread.Sleep(1000);
}
}

private void SetProgressBar(object sender, EventArgs e)
{
if (this.mLoadingProgressBar.Value < (this.mLoadingProgressBar.Maximum - 10))
this.mLoadingProgressBar.Value += 10;
else
this.mLoadingProgressBar.Value = 0;
}

The first time I execute this code, the SetProgressBar method gets
called one time, and the control is never repainted to reflect the new
value. If I invoke the Start method again, SetProgressBar never gets
called, and the application hangs.

Does anyone have any idea why this particular code might not be
working correctly? Or does anyone have any suggestions on a better
way to accomplish what I'm trying to do?

Thanks in advance.
Martin
 
Consider calling progressBar.Update() or better Application.DoEvents() from
inside SetProgressBar()
 
Hi,

Does the Start() method finish right after you create the thread?

If not that is where the problem is, y ou have to move ALL the processing to
the worker thread, you are not doing that, you are doing all the work IN the
main UI thread.

Changes are that when you finish the /* some long running processes */ you
get all the events

do like this:

private void Start()
{
this.mLoading = true;
Thread loadingThread = new Thread(new ThreadStart(AnimateProgressBar));
loadingThread.Start();
}

private void Done(object sender, EventArgs e)
{
this.Close() ; or what ever you want
}
private void AnimateProgressBar()
{
/* some long running processes */

this.mLoadingProgressBar.Invoke(new EventHandler(this.SetProgressBar));


}

IF the /* some long running processes */ can not be interrupted ( dataset
being loaded ) you have to create a timer for the progressbar.

Just let me know if you need some working code.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



martin said:
I'm trying to display a progress bar while some background processing
is done in my PPC app. I am using Control.Invoke, but it's not
working quite right. Here's a code snippet:

private void Start()
{
this.mLoading = true;
Thread loadingThread = new Thread(new ThreadStart(AnimateProgressBar));
loadingThread.Start();

/* some long running processes */

this.mLoading = false;
}

private void AnimateProgressBar()
{
while (this.mLoading)
{
this.mLoadingProgressBar.Invoke(new EventHandler(this.SetProgressBar));
// i've experimented with Thread.Sleep()
// Thread.Sleep(1000);
}
}

private void SetProgressBar(object sender, EventArgs e)
{
if (this.mLoadingProgressBar.Value <
(this.mLoadingProgressBar.Maximum - 10))
 
Back
Top