Re: how to just wait a few seconds for a callback to happen?
Re: how to just wait a few seconds for a callback to happen?
- Subject: Re: how to just wait a few seconds for a callback to happen?
- From: Becky Willrich <email@hidden>
- Date: Mon, 2 May 2005 09:08:01 -0700
After I send the message, how can I just have the client wait until
it receives its info back? And after a second or two, if nothing
comes, to just proceed without it? Is there any way to run the run
loop for a couple of seconds in order to give the server a chance
to respond, then go on with my setup? I want to avoid setting up
once and then setting up again when a callback function comes through.
Just run the runloop manually after setting up the CFSocket and
scheduling it on the runloop. Look at CFRunLoopRunInMode(), which
has a timeout argument. The normal way to do this is to call
CFRunLoopRunInMode in a loop, having it return after each source is
handled. Your callback should set some state detectable from the
callsite for CFRunLoopRunInMode(), so it can tell whether it's done
waiting:
// the CFSocket should have its callback set and be scheduled on the
current run loop before entering this loop
// Also, the callback needs to be able to set done to TRUE, to cause
the loop below to exit.
Boolean done = FALSE;
CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
CFAbsoluteTime timeout = now + kTimeout; // kTimeout is however long
you want to wait
while (!done && now < timeout) {
CFTimeInterval timeRemaining = timeout - now;
CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeRemaining, TRUE);
now = CFAbsoluteTimeGetCurrent();
}
if (!done) {
// Timeout occurred
} else {
// Got the expected socket traffic
}
Note that by running the runloop in the default mode, you are
allowing any other run loop processing to proceed (for instance, if
you have another socket scheduled as well). If you want to make sure
nothing but waiting on this particular socket happens, you should
schedule the socket on a private mode, the run the run loop in that
mode.
Hope that helps,
REW
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden