Suspending main run loop waiting for DO callback
Suspending main run loop waiting for DO callback
- Subject: Suspending main run loop waiting for DO callback
- From: James Bucanek <email@hidden>
- Date: Thu, 30 Jun 2005 09:01:02 -0700
Hello again,
I've got a situation where I'd like to "suspend" the main run loop (i.e. all GUI events) until a Distributed Objects message is received. Can this be done?
To explain:
My main run loop needs to pre-flight an operation, which may present a series of modal dialogs. If successful, I want to return to the calling function so the actual operation can begin.
The bad news is that the pre-flighting code is part of another process which communicates with the GUI application via asynchronous callbacks. Normally this works like magic, because the application just starts a process and goes back to doing GUI stuff. But in this case, I have several steps that need to be started and checked in sequence before moving on to the next step. What I want to do is something like this:
+ (BOOL)preflightOperation
{
BOOL readyToRun = NO;
while (!readyToRun)
{
Start_preflight_step1(); // starts in separate process
Wait_for_callback();
if (preflight_failed)
{
if (ask_to_continue_cancel_etc())
continue;
else
break;
}
Start_preflight_step2(); // starts in separate process
Wait_for_callback();
if (preflight_failed)
{
if (ask_to_try_again_cancel_etc())
continue;
else
break;
}
readyToRun = YES;
}
return (readyToRun);
}
static BOOL preflightDone;
static void Wait_for_callback()
{
preflightDone = NO;
BOOL isRunning;
do
{
NSDate* soon = [NSDate dateWithTimeIntervalSinceNow:0.5];
isRunning = [[NSRunLoop currentRunLoop]
runMode:NSConnectionReply
before:soon];
} while (isRunning && !preflightDone);
}
The preflight steps would start a process which would eventually dispatch a message to another object when it is done. That method would then "break" the running loop so the preflight method could continue executing:
- (void)preflightDidFinish:(id)data
{
preflightDone = YES;
}
Will this work? Is NSConnectionReply the correct run loop mode to use here? Is there a cleaner way of stopping the run loop besides periodically polling for a value?
I have a fall-back plan that involves starting a second thread. That thread starts the pre-flighting steps, waits for the response message, while the main run loops suspends on a semaphore. Not as simple, but I'm pretty sure it will work.
--
James Bucanek <mailto:email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden