如何创建消息框线程?

发布于 2024-12-23 03:51:07 字数 370 浏览 0 评论 0原文

我尝试过做类似的事情...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

糖果控 2024-12-30 03:51:07

考虑将消息文本作为线程参数而不是全局变量传递,以使代码线程安全。

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)。

如果您的目标是现代 Windows 操作系统,最好定义 UNICODE,因为 MessageBoxA 会将字符串转换为 UTF-16 并调用 MessageBoxW

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);

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, because MessageBoxA will convert you strings to UTF-16 and call MessageBoxW

淡笑忘祈一世凡恋 2024-12-30 03:51:07

为消息框创建线程而不出现故障的最佳方法是什么?

一般来说,你不需要。

在 Windows 下,应用程序中的所有窗口(小“w”,此处表示单独的 GUI 元素)都在单个线程中“运行”。这通常是“主”线程 - 但特别是,它是消息泵运行的线程。有关消息泵的更多详细信息,请参阅我的链接帖子。

如果您真正想要实现的是一个对话框或某种其他类型的窗口,可以与其他正在运行的窗口同时运行,以便在其他窗口仍然响应用户输入时它可以保持运行,那么您需要一个 无模式窗口或对话框。该窗口不会阻止其他窗口处理更新或接受用户输入。 请参阅此处 有关如何使用 MFC 实现此功能的说明,但请注意,您不需要使用 MFC 来实现无模式对话框。

What is the best way to create a thread for a message box, without having it glitch up?

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.

一花一树开 2024-12-30 03:51:07

老问题,新答案...

如果正在调用的线程是由立即退出的进程调用的(假设您正在运行一个使用 VC++ DLL 的可执行文件,它会弹出一个消息框),那么该线程将永远不会有一个执行的机会。

我创建了一个用于测试的 VC++ 可执行文件,它在我的 DLL 上运行加载库,并且 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.

  • I was informed by a coworker that I needed to add a sleep to the end of the testing executable that is using the DLL file in order for it to 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文