OMG. I got it to work. Using a determinate progress bar even. I feel like I just invented the light bulb.
.xib has determinate progress bar with min: 0 max: 100... and a run button and status label.
#import "RootViewController.h"
@interface RootViewController ()
@property (weak) IBOutlet NSProgressIndicator *progressIndicator;
@property (weak) IBOutlet NSTextField *statusLabel;
@property (weak) IBOutlet NSButton *runButtonOutlet;
@end
@implementation RootViewController
- (IBAction)runButton:(NSButton *)sender
{
[self.statusLabel setStringValue:@"Working ..."];
[self.runButtonOutlet setEnabled:NO];
[_progressIndicator startAnimation:nil];
dispatch_queue_t queue = dispatch_queue_create("com.sample", 0);
dispatch_queue_t main = dispatch_get_main_queue();
dispatch_async(queue,
^{
[self performLongRunningWork];
dispatch_async(main, ^{ [self workDone]; });
});
}
- (void)performLongRunningWork //finds large prime numbers between two numbers, takes about 15 seconds.
{
long i, j, flag, temp = 0;
int count = 0;
long num1 = 20000000;
long num2 = 20001000;
temp = num1;
if ( num1 % 2 == 0)
{
num1++;
}
for (i = num1; i <= num2; i = i + 2)
{
flag = 0;
for (j = 2; j <= i / 2; j++)
{
if ((i % j) == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
printf("%ld\n", i);
count++;
dispatch_async(dispatch_get_main_queue(), ^{
[self.progressIndicator incrementBy:1.00];
});
}
}
printf("Number of primes between %ld & %ld = %d\n", temp, num2, count);
}
- (void)workDone
{
[self.statusLabel setStringValue:@"Done ..."];
[self.runButtonOutlet setEnabled:YES];
[_progressIndicator stopAnimation:nil];
}