GetMessage()如何工作? [英] How does GetMessage() work?

查看:105
本文介绍了GetMessage()如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试应用程序,并在WinMain中对消息框(作为断点)进行了调用.在某些时候,我阻止了"while(GetMessage())"循环的执行,但是我的应用程序运行良好!为了进一步测试,我完全删除了循环:

I was debugging an application and placed calls to message boxes (as break points) in the WinMain. At some point I prevented the "while (GetMessage())" loop from executing, but my application worked fine! To test it further, I completely removed the loop:

#pragma argsused
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
MSG msg;
if (!hPrevInstance)
    if (!InitApplication(hInstance)) return 0;
if (!InitInstance(hInstance, nCmdShow)) return 0;
/*
while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    };
*/
MessageBox(NULL, "About to return", "Debug", MB_OK);
return msg.wParam;
};



但是该应用程序仍然可以工作(只要我保持该消息框处于活动状态),而不仅仅是工作",就像显示一个简单的窗口一样,它还维护了串行通信,数据采集硬件,文件创建,注册表访问,图形等功能-一切都照常进行.
我一直确信主线程中的所有消息都将通过该GetMessage()循环,并且确实很容易通过在循环中放置陷阱来验证这一点.但是整个循环都被注释掉了-我不知道发生了什么.

//================================
在接受解决方案后,我将为以后的读者添加此部分:
我的代码中的消息框只能是系统创建的模式对话框,最有可能以与DialoBox()函数类似的方式工作.
根据Win32联机帮助,"[对话框]函数显示对话框…,禁用所有者窗口,最后启动其自己的消息循环以检索和分发该对话框的消息.
当对话框过程调用EndDialog函数时,DialogBox破坏对话框,结束消息循环,启用所有者窗口(如果先前已启用),并返回nResult参数……"

现在,对MessageBox()的调用对于父级具有NULL-这就是为什么我的主窗口(在对InitInstance的调用中创建)没有被禁用的原因.到目前为止,一切都很好:现在我们有了一个线程和一个消息循环.唯一的歧义仍然存在于短语启动其自己的消息循环以检索和分发对话框对话框的消息" –显然,不仅是对话框的,而且是所有窗口的



but the application still worked (as long as I kept that message box alive) - and not just "worked" - like showing a simple window - it maintained serial communications, data acquisition hardware, file creation, registry access, graphics and so on - it all worked as usual.
I have always been sure that all the messages in the main thread are going through that GetMessage() loop, and indeed, it is easy to verify that by putting a trap inside the loop. But with the entire loop commented out - I do not understand what is going on.

//==================================
I am adding this part for future readers, after the solutions are accepted:
A message box in my code must be nothing but a modal dialog box created by the system, most likely in a very similar way that DialoBox() function works.
According to Win32 Online Help, "The [DialogBox] function displays the dialog box …, disables the owner window, and finally starts its own message loop to retrieve and dispatch messages for the dialog box.
When the dialog box procedure calls the EndDialog function, DialogBox destroys the dialog box, ends the message loop, enables the owner window (if previously enabled), and returns the nResult parameter …"

Now, the call to MessageBox() has NULL for the parent - that is why my main window (created in the call to InitInstance) does not get disabled. So far so good: now we have a thread and a message loop within it. The only ambiguity remains in the phrase "starts its own message loop to retrieve and dispatch messages for the dialog box" – apparently, not just for the dialog box but for all windows created by the current thread.

推荐答案

每个线程都有一个消息循环,但是在应用程序中将第二个值设置为NULL的任何消息队列都将创建一个消息队列.对于当前线程.这意味着,即使您要删除消息循环(在第一次调用时会创建消息队列),您正在创建的MessageBox()仍可能在内部进行自己的消息循环,默认情况下会创建
Every thread has a message loop, but any message queue created within an application with that second value set to NULL, creates a message queue for the current thread. This means that even though you''re removing your message loop (that creates the message queue when it''s first called), the MessageBox() you''re creating is probably internally making its own message loop, which by default creates a message loop for the thread it''s operating on.


我为简单调试做同样的事情.
现在我知道我并不孤单.

无论如何,一个消息框会创建与您在代码中注释掉的消息框完全相同的消息循环.
这就是为什么您的应用程序保持相同方式运行的原因.
实际上,我见过来自极端编码器的应用程序,这些应用程序没有明确的消息循环.
它们只是显示一个消息框并运行相同.

这里有一些尝试可以增进您的理解.
一键创建GUI应用.
使用消息循环作为按钮的代码.
现在,每次您单击按钮时,应用程序都会进入一个消息循环,该循环更深一层.
关闭该应用程序意味着打破每个循环,这也意味着您必须多次单击关闭"按钮.

试试看还是想像一下.
无论哪种方式都可以.

干杯.
I do the same thing for simple debugging.
Now I know I''m not alone.

Anyway, a message box creates the exact same message loop as the one you commented out in your code.
That''s why your app keeps working the same way.

In fact, I''ve seen apps from extreme coders that don''t have explicit message loops.
They simply show a message box and run just the same.

Here''s something to try to improve your understanding.
Create a GUI app with one button.
Use your message loop as the code of the button.
Now, each time you click the button, the app goes into a message loop that is one level deeper.
Closing the app means breaking out of each loop, which also means that you have to click the Close button several times.

Try it or just imagine it.
It works either way.

Cheers.


这篇关于GetMessage()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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