How to refresh a progress indicator in a lengthy monolithic task ?
07/11/04 08:55
We need a visual feedback during a very long computing task but when we change value of a progress bar using setDoubleValue nothing happens until the computing task is done ?
That is normal setDoubleValue posts a refresh event using setNeedsDisplay:YES but the actual refresh will be done at the next run loop step. Since you are not calling the run loop from your calculation, the refresh is post-posed until the end.
Several solutions :
1. do your calculation in a separate thread if what you are doing is thread safe of course and don't call any routine of the OS martked by Apple as to be executed in the main thread (sending and receiving Apple Events, some Quartz and Quicktime functionnalities, etc.).
2. call directly display on the controls requiring a refresh;
3. embed your calculation in a modal session
NSModalSession modalSession = [NSApp beginModalSessionForWindow:myWindow] ;
jobDone = NO ;
jobCanceled = NO ;
while (!(jobDone || jobCanceled))
{
if ([NSApp runModalSession: modalSession] != NSRunContinuesResponse) {
jobCanceled = YES ;
break;
}
// do the next job's step...
[myProgressBar setDoubleValue:some_value];
}
[NSApp endModalSession: modalSession] ;
Several solutions :
1. do your calculation in a separate thread if what you are doing is thread safe of course and don't call any routine of the OS martked by Apple as to be executed in the main thread (sending and receiving Apple Events, some Quartz and Quicktime functionnalities, etc.).
2. call directly display on the controls requiring a refresh;
3. embed your calculation in a modal session
NSModalSession modalSession = [NSApp beginModalSessionForWindow:myWindow] ;
jobDone = NO ;
jobCanceled = NO ;
while (!(jobDone || jobCanceled))
{
if ([NSApp runModalSession: modalSession] != NSRunContinuesResponse) {
jobCanceled = YES ;
break;
}
// do the next job's step...
[myProgressBar setDoubleValue:some_value];
}
[NSApp endModalSession: modalSession] ;