locked
Passing window handle to a worker thread RRS feed

  • Question

  • Hi,

    I have created a worker thread on button click event and passed the window handle to it.

    The worker thread only changes the title of the window but actually it hangs the window without changing the title of it.

    void CMyWnd::OnBtnClicked2()
    {
    	DATA* data = new DATA; 
    	data->n = 1;
    	data->hwnd = GetSafeHwnd();
    	CWinThread* pThread = AfxBeginThread(ThreadProc, data );
    	DWORD dwStatus = WaitForSingleObject(pThread->m_hThread, INFINITE);
    	switch(dwStatus)
    	{
    	case WAIT_OBJECT_0:
    		break;
    	case WAIT_TIMEOUT:
    		break;
    	case WAIT_FAILED:
    		break;
    	case WAIT_ABANDONED:
    		break;
    	}
    	delete data;
    }
    

    The DATA is a globally declared structure as,

    typedef struct _data
    	{
    		int n;
    		HWND hwnd;
    	}DATA;
    

    Code of the worker thread is as follows

    UINT ThreadProc(LPVOID param)
    {
    	CString str;
    	DATA* data = reinterpret_cast<DATA*>(param);
    	int t = 0;
    
    	while(t <= data->n)
    	{
    		if(::IsWindow(data->hwnd))
    		{
    			str.Format(L"%d", t++);
    			SetWindowText(data->hwnd, str.GetBuffer());
    			//((CFrameWnd*)CWnd::FromHandle(data->hwnd))->SetWindowTextW(str.GetBuffer());
    		}
    		::Sleep(100);
    	}
    	return 0;
    
    }
    

    Please let me know why it is not updating the title of the window and why it is hanging when we step over the SetWindowTest API.

     

    thanks

    code.

    Tuesday, January 11, 2011 6:17 AM

Answers

  • You problem is the "WaitForSingleObject" in your "CMyWnd::OnBtnClicked2" function. The "WaitForSingleObject" results in a wait until your Thread is stopped. The problem is that SetWindowText sends the message WM_SETTEXT to your "still waiting" main thread. You have a deadlock.

    For me "WaitForSingleObject" does not make sense in this context. Why waiting in the thread?

    You could modify your code in the followed way for example, the ThreadProc code from your code snippet is unchanged:

    // H file -----------------------------------------------------
    class CMyWnd : ...
    {
     ...
     DATA data; 
     ...
    }
    
    // CPP file ---------------------------------------------------
    void CMyWnd::OnBnClickedButton1()
    {
     data.n = 1;
     data.hwnd = m_hWnd;
     CWinThread* pThread = AfxBeginThread(ThreadProc, &data );
    }
    Tuesday, January 11, 2011 6:50 AM
  • With the changes I told you I do not have any problems, and the windows caption will be changed as expected. I tested it again with VC2008 a few minutes ago. I copied the code of your ThreadProc to my project and changed the button handler in the way I wrote you, and it works as expected.
    Tuesday, January 11, 2011 11:44 AM

All replies

  • You problem is the "WaitForSingleObject" in your "CMyWnd::OnBtnClicked2" function. The "WaitForSingleObject" results in a wait until your Thread is stopped. The problem is that SetWindowText sends the message WM_SETTEXT to your "still waiting" main thread. You have a deadlock.

    For me "WaitForSingleObject" does not make sense in this context. Why waiting in the thread?

    You could modify your code in the followed way for example, the ThreadProc code from your code snippet is unchanged:

    // H file -----------------------------------------------------
    class CMyWnd : ...
    {
     ...
     DATA data; 
     ...
    }
    
    // CPP file ---------------------------------------------------
    void CMyWnd::OnBnClickedButton1()
    {
     data.n = 1;
     data.hwnd = m_hWnd;
     CWinThread* pThread = AfxBeginThread(ThreadProc, &data );
    }
    Tuesday, January 11, 2011 6:50 AM
  • Bordon,

    Thanks for your quick reply. I tried after replacing the WaitForSingleObject and now it is not hanging the main window but still SetWindowText is not changing the title of the main window.

     

    Thanks,

     

    Tuesday, January 11, 2011 9:07 AM
  • With the changes I told you I do not have any problems, and the windows caption will be changed as expected. I tested it again with VC2008 a few minutes ago. I copied the code of your ThreadProc to my project and changed the button handler in the way I wrote you, and it works as expected.
    Tuesday, January 11, 2011 11:44 AM
  • Thanks Bordon,

    It is working :).

    Thursday, January 13, 2011 6:01 AM