如何创建消息框线程? [英] How do you create a Message Box thread?

查看:112
本文介绍了如何创建消息框线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试做类似...

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆