Delphi - application hangs while ShowMessage or MessageDlg

a 夏天 提交于 2021-02-08 09:14:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!