JoeB
Thu Nov 24 05:56:50 CST 2005
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
"Daniel Moth" <dmoth74@hotmail.com> wrote in message
news:OACtwZH8FHA.1140@tk2msftngp13.phx.gbl...
> When touching the UI lement you must use Control.Invoke:
>
http://www.danielmoth.com/Blog/2004/10/invoke-cf-and-full-fx.html
>
> Also consider the BackgroundWorker:
>
http://www.danielmoth.com/Blog/2004/12/backgroundworker-sample.html
>
> Cheers
> Daniel
> --
>
http://www.danielmoth.com/Blog/
>
> "JoeB" <joe@sdfdklshj.com> wrote in message
> news:OCqKvPF8FHA.3804@TK2MSFTNGP14.phx.gbl...
>> 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
>>
>>
>>
>>
>>
>>
>> "JoeB" <joe@sdfdklshj.com> wrote in message
>> news:upyzUvE8FHA.2036@TK2MSFTNGP14.phx.gbl...
>>> 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
>>>
>>
>>
>
>