Threads and Msgbox

Does a MsgBox called from a thread stop the thread until someone presses the OK button?

no, it raises an exception telling you not to call it on a thread.

Call a Timer from the Thread. In this Timer, show your Message.

PS: The Code in the Timer will pause while the Message is shown. :wink:

Expanding on what Christian and Sascha said AND assuming you’re using Xojo and not Real Studio…

Showing a MsgBox from a thread will throw a ThreadAccessingUIException.

Instead, show the message box from a timer and have that timer monitor a property. This could be a window property if your app has a windowed UI. If your app has no UI (headless) this could be a property of app or a module.

Since you’re showing a MsgBox, I’m guessing the app does have a UI, so probably best to use a Window property.

Also - Both threads and any other timers will continue running while the MsgBox is being displayed to the user however, the timer displaying the MsgBox will be blocked.

Here’s a small, quickly tossed together example.

Notice the second timer updating the label on the window and the thread keep running. The first timer is stopped because it’s waiting on the MsgBox to be dismissed before it continues executing code.


This is all true for a Desktop app, but not a WebApp. In a WebApp a MsgBox inside a thread is ignored (no exception) and will continue on. Outside a thread a MsgBox will not stop execution of your code.

Indeed, everything is asynchronous. Actually, halting execution with DelayMBS or a tight loop will impair not only the current sessions, but all sessions.

Assuming OS X, create an Applescript with an on run handler accepting a message
and tell application "System Events" to display dialog myMessage
Save it to your project (p.ex AS_DisplayDialog.scpt) and call it from within the thread
p.ex. AS_DisplayDialog("This is a message")
This will trigger a dialog box and will pause execution of the thread until you click OK, no exception thrown
This example will continue execution also if you click cancel, so if you actually want to use this you should investigate this path a little further. I actually only use it as a quick and dirty way when debugging, to quickly see a variable or to see until where execution works properly etc …

Calling system events in AppleScript is not compatible with MAS apps.