如何创建消息框线程? [英] How do you create a Message Box thread?
问题描述
我尝试做类似...
const char *MessageBoxText = "";
DWORD WINAPI CreateMessageBox(LPVOID lpParam){
MessageBox(NULL, MessageBoxText, "", MB_OK|MB_APPLMODAL);
return TRUE;
}
MessageBoxText = "Blah, Blah, Blah...";
CreateThread(NULL, 0, &CreateMessageBox, NULL, 0, NULL);
但是,对于我要执行的任务,这似乎无法正常工作. 在没有故障的情况下为消息框创建线程的最佳方法是什么?
However, this does not seem to work correctly for the task I am trying to perform. What is the best way to create a thread for a message box, without having it glitch up?
推荐答案
请考虑将消息文本作为线程参数而不是全局变量传递,以使代码成为线程安全的.
Consider passing message text as thread parameter instead of global variable, to make code thread-safe.
DWORD WINAPI CreateMessageBox(LPVOID lpParam) {
MessageBoxA(NULL, (char*)lpParam, "", MB_OK);
return 0;
}
CreateThread(NULL, 0, &CreateMessageBox, "Blah, Blah, Blah...", 0, NULL);
此外,您无需指定MB_APPLMODAL
作为默认标志(等于0).
Also, you don't need to specify MB_APPLMODAL
as it's default flag (it equals to 0).
如果您的目标是现代Windows操作系统,则最好定义UNICODE
,因为MessageBoxA
会将您的字符串转换为UTF-16并调用MessageBoxW
If you are targeting modern Windows OS, it's better to have UNICODE
defined, because MessageBoxA
will convert you strings to UTF-16 and call MessageBoxW
这篇关于如何创建消息框线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!