I notice in my program that when I call NSRunAlertPanel(), while it waits to return (the user has not yet clicked a button) NSTimer() does not fire. It fires regularly, perfectly until the NSRunAlertPanel() line, but then is not called again until the user clicked a button on the dialog.
Is there a way to keep the timer running, even while the dialog is on the screen?
The alert panel is starting its own event loop (via
-[NSApplication runModalForWindow:], separate from the (presumably main) event loop that the timer was originally scheduled on. This is to prevent the user from interacting with any other elements of the application until the alert is handled.Off the top of my head, there are two ways I can think of to make the timer still fire while the panel is presented: run the main run loop, and add the timer to the panel’s run loop. I’m honestly not sure how either of these will work out.
For the first, you can get the application’s main run loop easily enough:
[NSRunLoop mainRunLoop], and then tell it torunUntilDate:for a short time (less than a second) in the future. This will require you to set up a (while) loop, where you let the main run loop and the modal run loop run for a short while alternately. The problem here is that letting the main run loop be active will allow input to be processed, defeating the modality of the panel.For the second, you just take your reference to the timer and then do
[[NSRunLoop currentRunLoop] addTimer:forMode:]. What I’m not sure about here is how the timer’s fire date will interact with being re-added to another loop, but you can try it out.Hopefully I’m being an idiot and missing something really obvious, and another answer will appear shortly with the correct solution.