如何创建消息框线程?
我尝试过做类似的事情...
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);
但是,这对于我尝试执行的任务来说似乎无法正常工作。 为消息框创建线程而不出现故障的最佳方法是什么?
I have tried doing things like...
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑将消息文本作为线程参数而不是全局变量传递,以使代码线程安全。
此外,您无需指定
MB_APPLMODAL
,因为它是默认标志(等于 0)。如果您的目标是现代 Windows 操作系统,最好定义
UNICODE
,因为MessageBoxA
会将字符串转换为 UTF-16 并调用MessageBoxW
Consider passing message text as thread parameter instead of global variable, to make code thread-safe.
Also, you don't need to specify
MB_APPLMODAL
as it's default flag (it equals to 0).If you are targeting modern Windows OS, it's better to have
UNICODE
defined, becauseMessageBoxA
will convert you strings to UTF-16 and callMessageBoxW
一般来说,你不需要。
在 Windows 下,应用程序中的所有窗口(小“w”,此处表示单独的 GUI 元素)都在单个线程中“运行”。这通常是“主”线程 - 但特别是,它是消息泵运行的线程。有关消息泵的更多详细信息,请参阅我的链接帖子。
如果您真正想要实现的是一个对话框或某种其他类型的窗口,可以与其他正在运行的窗口同时运行,以便在其他窗口仍然响应用户输入时它可以保持运行,那么您需要一个 无模式窗口或对话框。该窗口不会阻止其他窗口处理更新或接受用户输入。 请参阅此处 有关如何使用 MFC 实现此功能的说明,但请注意,您不需要使用 MFC 来实现无模式对话框。
In general, you don't.
Under Windows, all the windows (small 'w', meaning individual GUI elements here) in an application "run" in a single thread. This is generally the "main" thread -- but in particular, it is the thread in which the message pump is running. See my linked post for more details on the message pump.
If what you are truly trying to achieve is a dialog box or some other kind of window that can run concurrently to other windows running, so that it can remain up while other windows are still responsive to user input, then you want a modeless window or dialog box. This is a window that doesn't block other windows from processing updates or accepting user input. See here for a description of how to implement this using MFC, but note that you don't need to use MFC in order to implement a modeless dialog box.
老问题,新答案...
如果正在调用的线程是由立即退出的进程调用的(假设您正在运行一个使用 VC++ DLL 的可执行文件,它会弹出一个消息框),那么该线程将永远不会有一个执行的机会。
我创建了一个用于测试的 VC++ 可执行文件,它在我的 DLL 上运行加载库,并且 DLL 创建了带有调试消息框的各种线程。由于我的测试可执行文件仅调用了 DLL,然后退出,因此它不起作用。
果然,它修复了它。这也是有道理的,尤其是在 DLL 的上下文中。大多数应用程序会一直循环,直到收到退出指示为止。我的测试应用程序没有。
请注意您的应用程序的行为方式!
Old question, new answer...
If the thread being invoked is being called into existence by a process that immediately exits (say you're running an executable that uses a VC++ DLL which pops a message box) then the thread will never have a chance to execute.
I had created a VC++ executable for testing that ran a loadlibrary on my DLL and the DLL created various threads with debugging message boxes. Since my testing executable only called the DLL and then exited, it didn't work.
Sure enough, it fixed it. This makes sense too, especially in the context of a DLL. Most applications loop until instructed to exit; my test application did not.
Be mindful of how your application is behaving!