问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/26718030/delphi-application-hangs-while-showmessage-or-messagedlg