博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在非主线程中创建窗口
阅读量:4546 次
发布时间:2019-06-08

本文共 4776 字,大约阅读时间需要 15 分钟。

很多朋友都会有过这样的经历,为什么在主线程中创建窗口且窗口工作很正常,但一移到非主线程(有的朋友喜欢叫它为工作线程),却无法正常工作.

   本文就这个问题和各位探讨,可能无法做到尽善尽美,但能抛砖引玉也算是欣慰了. 在主线程中创建一个能够正常工作的窗口,估计地球人都知道.
   这是一段工作正常的代码: 

#include \"windows.h\"

HWND g_hWnd = NULL;

HINSTANCE g_hInst;

LRESULT WndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)

{
    return DefWindowProc(hWnd,wMsg,wParam,lParam);
}

void CreateWnd(void)

{    

    WNDCLASS wc = {0};

    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = g_hInst;
    wc.hIcon         = NULL; 
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_WINDOW);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = TEXT(\"SimpleWindow\");

    RegisterClass(&wc);

    g_hWnd = CreateWindowEx(0,

                TEXT(\"SimpleWindow\"),
                TEXT(\"SimpleWindow\"),
                WS_VISIBLE,
                0,
                0,
                200,
                200,
                NULL,  [Page]
                NULL, 
                g_hInst, 
                0);
}

int WINAPI WinMain(    HINSTANCE hInstance,

                    HINSTANCE hPrevInstance,
                    LPTSTR    lpCmdLine,
                    int       nCmdShow)
{
     // TODO: Place code here.
    g_hInst = hInstance;

    CreateWnd();

    //The message loop    

    MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;

}
    如果我们创建一个线程,然后在这个线程中创建窗口,看看带给我们的是什么:
#include \"windows.h\"

HWND g_hWnd = NULL;

HINSTANCE g_hInst;

LRESULT WndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)

{
    return DefWindowProc(hWnd,wMsg,wParam,lParam);
}

void CreateWnd(void)

{    

    WNDCLASS wc = {0};

    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = g_hInst;
    wc.hIcon         = NULL; 
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);

wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_WINDOW);

    wc.lpszMenuName  = NULL;
    wc.lpszClassName = TEXT(\"SimpleWindow\");

    RegisterClass(&wc);

    g_hWnd = CreateWindowEx(0, [Page]

                TEXT(\"SimpleWindow\"),
                TEXT(\"SimpleWindow\"),
                WS_VISIBLE,
                0,
                0,
                200,
                200,
                NULL, 
                NULL, 
                g_hInst, 
                0);
}

DWORD CreateThread(PVOID pArg)

{
    CreateWnd();
    return 0;
}

int WINAPI WinMain(    HINSTANCE hInstance,

                    HINSTANCE hPrevInstance,
                    LPTSTR    lpCmdLine,
                    int       nCmdShow)
{
     // TODO: Place code here.
    g_hInst = hInstance;

    HANDLE hThrd = CreateThread(NULL,0,CreateThread,NULL,0,NULL);

    CloseHandle(hThrd);

    //The message loop    

    MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;

}

    我们似乎什么都没见到,只是窗口一闪,啥都没了.因为g_hWnd为全局变量,我们的理智告诉我们,在主线程没有退出之前,g_hWnd是不会销毁的.而用断点调试,将会发现在WndProc函数中只能接收WM_CREATE及以后一些消息,之后的再也收不到了,特别是WM_PAINT似乎就凭空消失了!那么,代码什么都没变更,只是移动到了分线程中,为何会出现这个问题呢? [Page]

    一切似乎很简单,在MSDN中我们找到了答案(原文见:http://support.microsoft.com/kb/90975/en-us):
    In a multithreaded application, any thread can call the CreateWindow() API to create a window. There are no restrictions on which thread(s) can create windows. 

It is important to note that the message loop and window procedure for the window must be in the thread that created the window. If a different thread creates the window, the window won’t get messages from DispatchMessage(), but will get messages from other sources. Therefore, the window will appear but won’t show activation or repaint, cannot be moved, won’t receive mouse messages, and so on.

    该段话大意是:窗口在任何线程中都可以创建,但消息循环必须要和创建窗口在同一线程,否则窗口将无法从DispatchMessage()获取任何消息!

    原来如此,最重要是这么一句:It is important to note that the message loop and window procedure for the window must be in the thread that created the window.
    好吧,那么我们在支线程中放置消息循环代码,看看是什么结果吧:

#include \"windows.h\"

HWND g_hWnd = NULL;

HINSTANCE g_hInst;

LRESULT WndProc(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam)

{
    return DefWindowProc(hWnd,wMsg,wParam,lParam);
}

void CreateWnd(void)

{    

    WNDCLASS wc = {0};

    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = g_hInst;
    wc.hIcon         = NULL;

wc.hCursor       = LoadCursor(NULL, IDC_ARROW);

    wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_WINDOW);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = TEXT(\"SimpleWindow\");

    RegisterClass(&wc);

    g_hWnd = CreateWindowEx(0,

                TEXT(\"SimpleWindow\"),
                TEXT(\"SimpleWindow\"), [Page]
                WS_VISIBLE,
                0,
                0,
                200,
                200,
                NULL, 
                NULL, 
                g_hInst, 
                0);
}

DWORD CreateThread(PVOID pArg)

{
    CreateWnd();

    //The message loop    

    MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;

}

int WINAPI WinMain(    HINSTANCE hInstance,

                    HINSTANCE hPrevInstance,
                    LPTSTR    lpCmdLine,
                    int       nCmdShow)
{
     // TODO: Place code here.
    g_hInst = hInstance;

    HANDLE hThrd = CreateThread(NULL,0,CreateThread,NULL,0,NULL);

    CloseHandle(hThrd);

    MSG msg;

    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;

}
    一切正常,如同在主线程创建一样!
    当然了,还有点需要注意的,在这个例子中,由于消息循环在主线程和分线程都分别存在,如果在WndProc()调用PostQuitMessage(),那么退出的也仅仅是分线程,而主线程还是会不停地在等待消息,从而导致程序无法正常退出.不过倒不用过分担心,和这个示例代码不同,在实际代码编写中,在主线程往往都会创建主窗口,而在这个主窗口消息处理函数调用PostQuitMessage()则完全可以让主线程正常退出.

    事实告诉我们,非主线程创建窗口也能工作正常,只要我们注意一点:消息循环必须要和创建窗口在同一线程。

转载于:https://www.cnblogs.com/MaxWoods/archive/2010/07/17/1779486.html

你可能感兴趣的文章
ThreadLocal
查看>>
mysql 根据一张表更新另一张表
查看>>
java 反射与JVM
查看>>
使用maven打包项目遇到错误: http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException...
查看>>
【IDEA】IDEA中部署的项目添加Tomcat自带的一些项目
查看>>
队列Q(Wannafly挑战赛19)
查看>>
前台数据Json的转换和后台的保存
查看>>
CCF - 201412-3 - 集合竞价
查看>>
bzoj4264: 小C找朋友
查看>>
Mysql表结构操作,crud操作
查看>>
用 Canvas 制作刮刮卡
查看>>
挂载光盘与rpm安装
查看>>
[Android学习系列18]线程,进程,异步的一些事
查看>>
腾讯 AI Lab 计算机视觉中心人脸 & OCR团队近期成果介绍(3)
查看>>
课堂练习-增加信息
查看>>
A little issue in Mathematical Thought from Ancient to Modern Times, Vol. 3
查看>>
Zabbix对接AD域
查看>>
django 将view视图中的对象传入forms表单验证模块中
查看>>
log4net配置
查看>>
中文词频统计及词云制作
查看>>