1

why the application hangs when Executing Thread and that thread have ShowMessage or MessageDlg but when using MessageBox everything is working normally.

all this happened if the application Appearance not the defualt one >> "Windows"

if the selected Appearance "Windows" it will never hangs even with the ShowMessage and MessageDlg

3
  • 2
    If you want to get help on this, you must provide a sample code to reproduce the issue.
    – RRUZ
    Nov 3, 2014 at 16:04
  • 6
    Threads other than the main thread should not be accessing the UI. What's the point of doing so? If the thread has to stop for user input, there's no point in making it a separate thread. (And using ShowMessage or MessageDlg from a thread violates the "don't access visual controls from other than the main thread" rules. MessageBox doesn't because it's a WinAPI function call, and therefore isn't accessing VCL controls.)
    – Ken White
    Nov 3, 2014 at 16:12
  • What happens if you show a modal dialog from the thread and one from the main thread? Or one from multiple worker threads? What do you think happens then? Nov 3, 2014 at 18:23

1 Answer 1

4

ShowMessage() and MessageDlg() are not thread-safe. They display VCL Forms, which must only be used in the context of the main UI thread.

Windows.MessageBox() is generally thread-safe, if you specify a nil owner window when calling it from a worker thread. It creates and displays its own dialog window, and runs its own modal message loop, within the context of the calling thread, so there are usually no threading issues. But there are some gotchas (see this article: MessageBoxes and worker threads).

TApplication.MessageBox() calls Windows.MessageBox() internally, but is not thread-safe because it does things invoking the RTL and MainForm that are not thread-safe, and thus should only be used in the context of the main UI thread as well.

In short, don't use VCL popup messages in worker threads - period. Use Windows.MessageBox(), or to be on the safe side, delegate you popup messages to the main UI thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.