在非主线程中创建窗口

非主线程创建窗口的正确方法

//========================================================================
//TITLE:
//    在非主线程中创建窗口
//AUTHOR:
//    norains
//DATE:
//    Saturday  29-December-2007
//Environment:
//    VS2005 + SDK-WINCE5.0-MIPSII   
//========================================================================

   很多朋友都会有过这样的经历,为什么在主线程中创建窗口且窗口工作很正常,但一移到非主线程(有的朋友喜欢叫它为工作线程),却无法正常工作.本文就这个问题和各位探讨,可能无法做到尽善尽美,但能抛砖引玉也算是欣慰了.
   
   在主线程中创建一个能够正常工作的窗口,估计地球人都知道.
   
   这是一段工作正常的代码: 


#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, 
                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,
                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似乎就凭空消失了!那么,代码什么都没变更,只是移动到了分线程中,为何会出现这个问题呢?
    
    一切似乎很简单,在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"),
                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()则完全可以让主线程正常退出.
    

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


地址:http://blog.youkuaiyun.com/norains/article/details/2023957

在主线程创建定时器是许多 GUI 应用程序中常见的需求,特别是在需要周期性执行某些任务(如更新界面、数据采集、状态检测等)时。在 Qt 框架中,可以通过 `QTimer` 类实现定时器功能,而在 C# 或 Win32 API 中也有相应的定时器机制。 ### Qt 中在主线程创建定时器的方法 在 Qt 应用程序中,主线程通常负责处理用户界面事件。因此,使用 `QTimer` 时,如果定时器和槽函数都在主线程中运行,可以确保事件循环的正常执行。 #### 基本实现步骤: 1. 创建一个 `QTimer` 对象,并设置定时器类型和间隔时间。 2. 将定时器与对应的槽函数连接。 3. 调用 `start()` 方法启动定时器。 示例代码如下: ```cpp QTimer* timer = new QTimer; timer->setInterval(1000); // 设置间隔为1秒 connect(timer, &QTimer::timeout, this, &MyClass::onTimerTimeout); timer->start(); // 启动定时器 ``` 在该示例中,`onTimerTimeout` 是定时器超时时执行的槽函数。由于 `QTimer` 和事件循环都在主线程中运行,因此不会引发跨线程问题[^2]。 #### 定时器类型设置 Qt 提供了多种定时器类型,可以通过 `setTimerType()` 方法设置。例如: ```cpp timer->setTimerType(Qt::PreciseTimer); // 精确定时 ``` ### C# 中的主线程定时器实现 在 C# 中,可以使用 `System.Windows.Forms.Timer` 控件来实现主线程中的定时任务。该控件与 UI 线程绑定,适合用于更新界面元素。 示例代码: ```csharp System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); timer.Interval = 1000; // 设置间隔为1秒 timer.Tick += new EventHandler(OnTimerTick); timer.Start(); // 启动定时器 ``` 在 `OnTimerTick` 方法中可以执行更新控件的操作: ```csharp private void OnTimerTick(object sender, EventArgs e) { label1.Text = DateTime.Now.ToString("HH:mm:ss"); } ``` 需要注意的是,这种定时器依赖于主线程的消息循环,因此如果主线程被阻塞,定时器将无法正常工作[^4]。 ### Win32 API 中的主线程定时器 在 Win32 API 中,可以使用 `SetTimer` 函数创建定时器,并通过窗口过程处理 `WM_TIMER` 消息来响应定时事件。 示例代码: ```cpp SetTimer(hWnd, 1, 1000, NULL); // hWnd 是窗口句柄,1 是定时器 ID,1000 是间隔时间(毫秒) // 在窗口过程中处理 WM_TIMER 消息 case WM_TIMER: if (wParam == 1) { // 定时器逻辑 } break; ``` 这种方式的定时器也绑定在创建它的线程上,通常用于主线程中的界面更新或周期性任务[^5]。 ### 注意事项 - **避免阻塞主线程**:无论使用哪种框架,都应避免在主线程中执行长时间任务,否则可能导致界面卡顿甚至无响应。 - **定时器与事件循环**:定时器依赖于事件循环的运行。在 Qt 中,确保在主线程中调用 `QApplication::exec()`;在 C# 中,确保窗体应用程序的主循环正常运行;在 Win32 中,确保消息循环正常执行。 - **资源释放**:在不再需要定时器时,应调用 `stop()` 或相应的释放函数,避免资源泄漏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值