从PostQuitMessage的异常现象 想到的消息处理函数的异步和同步问题

来源:百度知道 编辑:UC知道 时间:2024/05/25 00:11:58
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
static TCHAR szAppName[]=TEXT("Hello Win");
static TCHAR szClassName[]=TEXT("HelloWinClass");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style =CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc =WndProc;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor =LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szClassName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("This Program requires Windows NT"),szAppName,MB_ICONERROR);
return 0;

}

这是MSDN上有关WM_QUIT的解释
This message does not have a return value because it causes the message loop to terminate before the message is sent to the application's window procedure.

PostQuitMessage立即返回到MessageBox执行成功还是需要点时间的,这点时间已经足够让KERNEL把你的线程KILL了,即使刚好进入MessageBox代码处也没办法;;;不过按你那种做法理论上是应能显示出来的,但需要一个前提,系统内核的调度必须要足够慢……一般都不可能吧;;如果你反汇编跟踪一下,你会发现MessageBox的显示其实还是挺复杂的。
==============================================
应该都不是那样,MessageBox是有执行到,但没显示,因为消息队列里有WM_QUIT消息了~~你跟踪一下会发现,MessageBox直接返回了