【问题标题】:Why is DestroyWindow blocked?为什么 DestroyWindow 被阻止?
【发布时间】:2021-07-28 08:24:41
【问题描述】:

我在子线程中创建了一个窗口和消息循环。 当我通过PostMessage发送自定义消息时,当我在线程中调用DestroyWindow时,DestroyWindow被阻塞并没有触发WM_DESTORY,所以消息循环线程无法正常退出。 我试过PostThreadMessage,另外我试过用closewindow。但问题是在调用DestroyWindow 后不会触发WM_DESTORY,而不是PostMessage。 消息循环线程仍在运行且窗口句柄有效,为什么?折腾了几天,找不到原因,非常感谢。

销毁窗口:

#define WM_QUIT_MSG_LOOP (WM_USER+8600)
mfxStatus CD3D11Device::DeleteRenderChildWindow()
{
    LOG(LS_INFO) << "Intel D3D11Render, Enter DeleteRenderChildWindow, HWND:" << m_hChildHwnd;

    //SetParent(m_hChildHwnd, NULL);
    if (m_hChildHwnd){
        LOG(LS_WARNING) << "Intel D3D11Render, DeleteRenderChildWindow, HWND:" << m_hChildHwnd;
        PostMessage(m_hChildHwnd, WM_QUIT_MSG_LOOP, NULL, NULL);
    }
    
    if(m_pChildWindowMsgThread)
        m_pChildWindowMsgThread->Wait();

    MSDK_SAFE_DELETE(m_pChildWindowMsgThread);
    MSDK_SAFE_DELETE(m_pCreateFinishEvent);
    LOG(LS_INFO) << "Intel D3D11Render, Leave DeleteRenderChildWindow, HWND:" << m_hChildHwnd;

    return MFX_ERR_NONE;
}

创建窗口:

 LRESULT CALLBACK CD3D11Device::ChildRenderMsgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
        LRESULT lResult = 0;
        switch (uMsg) {
        case WM_DESTROY: {
            LOG(LS_WARNING) << "Intel D3D11Render ChildRenderMsgProc, PostQuitMessage, HWND:" << hwnd;
            PostQuitMessage(0);
            break;
        }
        case WM_SETCURSOR: {
            break;
        }
        default:
            lResult = DefWindowProc(hwnd, uMsg, wParam, lParam);
            break;
        }
    
        return lResult;
    }
    
    HWND CD3D11Device::ThreadCreateChildWindow(){
        HMODULE hInstance = nullptr;
        BOOL result =
            GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
                GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
                reinterpret_cast<char*>(&DefWindowProc), &hInstance);
    
        if (!result) {
            LOG(LS_ERROR) << "[ThreadCreateChildWindow]GetModuleHandleExA failed.";
            return 0;
        }
    
        // Register the host window class. See the MSDN documentation of the
        WNDCLASSEXW wcex = {};
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.lpfnWndProc = &ChildRenderMsgProc;
        wcex.hInstance = hInstance;
        wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
        wcex.lpszClassName = _T("Render Window Class");
        //wcex.style |= CS_HREDRAW | CS_VREDRAW &~WS_CAPTION &~WS_SYSMENU;
    
        // Ignore the error which may happen when the class is already registered.
        RegisterClassExW(&wcex);
    
        RECT rcClient = { 0 };
        GetWindowRect(m_HandleWindow, &rcClient);
        // Create the host window.
        HWND hChildWindow =
            CreateWindowW(_T("Render Window Class"), _T("MiniRender"), WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 0,
                0, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, m_HandleWindow, nullptr, hInstance, nullptr);
        if (!hChildWindow) {
            LOG(LS_ERROR) << "[ThreadCreateChildWindow]Create child window failed.";
            return 0;
        }
    
        ShowWindow(hChildWindow, SW_SHOW);
        SetRenderChildHwnd(hChildWindow);
        m_pCreateFinishEvent->Signal();
        LOG(LS_WARNING) << "Intel D3D11Render, ThreadCreateChildWindow, HWND:" << hChildWindow;
    
        return hChildWindow;
    }

消息循环线程:

unsigned int CD3D11Device::ChildWindowMsgThread(void* ctx){
        CD3D11Device* pD3D11Device = static_cast<CD3D11Device*>(ctx);
    
        HWND hChildWindow = NULL;
        if (pD3D11Device) {
            hChildWindow = pD3D11Device->ThreadCreateChildWindow();
        }
    
        if (hChildWindow == NULL){
            return 0;
        }
        
        MSG msg;
        BOOL result;
        while ((result = GetMessage(&msg, NULL, 0, 0)) != 0) {
            if (result == -1) {
                LOG(LS_ERROR) << "Intel D3D11Render, ChildWindowMsgThread, GetMessage failed, HWND:" << hChildWindow;
                continue;
            }
    
            if (msg.message == WM_QUIT_MSG_LOOP) {
                LOG(LS_WARNING) << "Intel D3D11Render, ChildWindowMsgThread, recv WM_QUIT_MSG_LOOP, HWND:" << hChildWindow;

//这里堵住了

                DestroyWindow(hChildWindow);
            }
            else {
                PostMessage(pD3D11Device->GetParentHwnd(), msg.message, msg.wParam, msg.lParam);
            }
    
            LOG(LS_WARNING) << "Intel D3D11Render, ChildWindowMsgThread, GetMessageing, HWND:" << hChildWindow;
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return 0;
    }

【问题讨论】:

  • 当应用程序阻塞时。在调试器中点击 Break All。检查尝试调用 DestroyWindow 的线程并观察调用堆栈。

标签: c++ winapi message-loop


【解决方案1】:

窗口具有线程亲和性。只有创建窗口的线程才能销毁窗口(并为窗口接收和发送消息)。这在documentation中有明确说明:

线程不能使用 DestroyWindow 来销毁由不同线程创建的窗口。

【讨论】:

  • 提前致谢。在创建窗口的线程中调用 DestroyWindow。当收到“WM_QUIT_MSG_LOOP”的自定义消息时,您可以在 CD3D11Device::ChildWindowMsgThread 中看到 DestroyWindow。当我需要破坏这个窗口时,我将“WM_QUIT_MSG_LOOP”发送到消息循环中。
  • 根据DestroyWindow() 文档:“如果函数成功,则返回值非零。如果函数失败,则返回值为零。要获取扩展错误信息,请调用 GetLastError。 " DestroyWindow()GetLastError() 实际上返回了什么?你确定DestroyWindow() 真的被调用了吗?代码将WM_QUIT_MSG_LOOP 发布到m_hChildHwnd,但m_hChildWnd 分配在哪里?在SetRenderChildHwnd()?显示的代码几乎不是minimal reproducible example
猜你喜欢
  • 2018-04-18
  • 2011-07-03
  • 2018-08-17
  • 2020-01-29
  • 2021-09-12
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多