progress bar lockup

J

JoeB

Hi,

I posted this in the dotnet.csharp forum, but was told to come here to the
CF forum

This code does not run, the application throws when i call Invoke;


private delegate void DelegateStarter();

private void CycleProgressBar()
{
int x = 0;
while( true )
{
progressBar1.Value = x;
x += 10;
if( x > 100 )
x = 0;
Thread.Sleep(200);
}
}

private void button1_Click(object sender, System.EventArgs e)
{
DelegateStarter dls = new DelegateStarter( CycleProgressBar );
progressBar1.Invoke( dls ); // *** THROWS ***
}



Appereently, a progressbar cannot be updated on the main thread, so i need
to Control.Invoke to do the updating, but cant get it working. Can someone
help me?



Joe
 
J

JoeB

Right, i have managed to get the progress bar to update without stopping
when the mouse moves over it. However, the button can never be pressed.

One of the C# people here has told me it is not possible to start a thread
that updates a progress bar in the background and still allow the
application to respond to user input.

This is totally outragous! and cannot be true, but from what i have found
today, it is infact true. Here is my code, when the button is pressed the
progrress bar should be looped, press again and it should stop - however,
after starting the looping, the application never responds to mouse presses
again.




private bool bRun = false;

private void CycleProgressBar( object sender, EventArgs e )
{
int x = 0;
while( bRun )
{
progressBar1.Value = x;
x += 10;
if( x > 100 )
x = 0;
Thread.Sleep(400);
}
}


private void button1_Click(object sender, System.EventArgs e)
{
if( bRun == true )
{
bRun = false;
return;
}
bRun = true;
progressBar1.Invoke( new EventHandler(CycleProgressBar) );
}





j
 
J

JoeB

i've solved the porblem, i have a global var that holds the required
progress bar value, then Invoke a single delegate that reads the value.

private int ProgressBarValue = 0;

private void _UpdateProgressBar( object sender, EventArgs e )
{
ProgressBar.Value = ProgressBarValue;
}

private void UpdateProgressBar( int iPercent )
{
if( iPercent > 100 )
iPercent = 100;
else if( iPercent < 0 )
iPercent = 0;

ProgressBarValue = iPercent;
ProgressBar.Invoke( new EventHandler(_UpdateProgressBar) );
}

private void CycleProgressBar()
{
int x = 0;
while( bCycleProgressBarRunning )
{
UpdateProgressBar( 10 * x );
if( ++x > 10 )
x = 0;

Thread.Sleep(200);
}
UpdateProgressBar(0);
}

Thanks



j
 

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