Home > Software engineering >  Ask: why does the child thread is created in the window, when they call DestoryWindow jam?
Ask: why does the child thread is created in the window, when they call DestoryWindow jam?

Time:05-05

I created a window in the child thread and message loop,
When I sent via PostMessage custom message, hope the message loop in the thread exits, but when I received my custom message in the thread and call the DestroyWindow, DestroyWindow blocked and there is no trigger WM_DESTORY, so the message loop threads cannot normal exit,
My question is: what circumstances DestroyWindow blocks? Conflict with other news? Such as MoveWindow? Someone had a similar situation?
The code is as follows:
Destruction of window:
 
# define WM_QUIT_MSG_LOOP (WM_USER + 8600)
MfxStatus CD3D11Device: : DeleteRenderChildWindow ()
{
The LOG (LS_INFO) & lt; <"Intel D3D11Render, Enter DeleteRenderChildWindow HWND," & lt;
//SetParent (m_hChildHwnd, NULL);
If (m_hChildHwnd) {
The LOG (LS_WARNING) & lt; <"Intel D3D11Render DeleteRenderChildWindow, HWND:" & lt; PostMessage (m_hChildHwnd WM_QUIT_MSG_LOOP, NULL, NULL);
}

If (m_pChildWindowMsgThread)
M_pChildWindowMsgThread - & gt; Wait ();

MSDK_SAFE_DELETE (m_pChildWindowMsgThread);
MSDK_SAFE_DELETE (m_pCreateFinishEvent);
The LOG (LS_INFO) & lt; <"Intel D3D11Render, Leave DeleteRenderChildWindow HWND," & lt;
Return MFX_ERR_NONE;
}

Create a window:
 
LRESULT a CALLBACK CD3D11Device: : ChildRenderMsgProc (HWND HWND, UINT uMsg, WPARAM WPARAM, LPARAM LPARAM) {
LRESULT LRESULT=0;
The switch (uMsg) {
Case WM_DESTROY: {
The LOG (LS_WARNING) & lt; <"Intel D3D11Render ChildRenderMsgProc PostQuitMessage, HWND:" & lt; The 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 & lt; Char * & gt; (& amp; DefWindowProc), & amp; HInstance);

if (! The result) {
The LOG (LS_ERROR) & lt; <"[ThreadCreateChildWindow] GetModuleHandleExA failed.";
return 0;
}

//Register the host window class. See the MSDN documentation of the
WNDCLASSEXW wcex={};
Wcex. CbSize=sizeof (modifed WNDCLASSEX);
Wcex. LpfnWndProc=& amp; ChildRenderMsgProc;
Wcex. HInstance=hInstance;
Wcex. HCursor=LoadCursor (nullptr, IDC_ARROW);
Wcex. LpszClassName=_T (" Render Window Class ");
//wcex. Style |=CS_HREDRAW | CS_VREDRAW & amp; ~ WS_CAPTION & amp; ~ WS_SYSMENU;

//Ignore the error which may happen when the class is already registered.
RegisterClassExW (& amp; Wcex);

The RECT rcClient={0};
GetWindowRect (m_HandleWindow, & amp; RcClient);
//Create the host window.
HWND hChildWindow=
CreateWindowW (_T (" Render Window Class "), _T (" MiniRender "), the WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 0,
0, rcClient. Right - rcClient. RcClient. Bottom - rcClient. Left, top, m_HandleWindow, nullptr, hInstance, nullptr);
if (! HChildWindow) {
The LOG (LS_ERROR) & lt; <"[ThreadCreateChildWindow] Create the child window failed.";
return 0;
}

ShowWindow (hChildWindow, SW_SHOW);
SetRenderChildHwnd (hChildWindow);
M_pCreateFinishEvent - & gt; Signal ();
The LOG (LS_WARNING) & lt; <"Intel D3D11Render ThreadCreateChildWindow, HWND:" & lt;
Return hChildWindow;
}

Message loop threads:
 
Unsigned int CD3D11Device: : ChildWindowMsgThread (void * CTX) {
CD3D11Device * pD3D11Device=static_cast & lt; CD3D11Device * & gt; (CTX);

HWND hChildWindow=NULL;
If (pD3D11Device) {
HChildWindow=pD3D11Device - & gt; ThreadCreateChildWindow ();
}

If (hChildWindow==NULL) {
return 0;
}

MSG MSG.
BOOL result;
While ((result=GetMessage (& amp; MSG, NULL, 0, 0))!=0) {
If (result==1) {
The LOG (LS_ERROR) & lt; <"Intel D3D11Render ChildWindowMsgThread, GetMessage failed, HWND:" & lt; continue;
}

If (MSG) message==WM_QUIT_MSG_LOOP) {
The LOG (LS_WARNING) & lt; <"Intel D3D11Render ChildWindowMsgThread, recv WM_QUIT_MSG_LOOP, HWND:" & lt; //DestroyWindow block, according to??
The DestroyWindow (hChildWindow);
}
The else {
PostMessage (pD3D11Device - & gt; GetParentHwnd (), MSG. Message, MSG. WParam, MSG. LParam);
}

The LOG (LS_WARNING) & lt; <"Intel D3D11Render ChildWindowMsgThread, GetMessageing HWND:" & lt; TranslateMessage (& amp; MSG);
DispatchMessage (& amp; MSG);
}

return 0;
}
  • Related