Posted By

jimfred on 11/14/08


Tagged

c gui thread mfc


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

khouser


ASSERT_GUI_THREAD - see if current thread is GUI thread in MFC C++


 / Published in: C++
 

  1. inline void ASSERT_GUI_THREAD()
  2. {
  3. const CWnd * pWnd = AfxGetMainWnd();
  4. const HWND hWnd = pWnd->GetSafeHwnd();
  5. const DWORD WindowThreadId = GetWindowThreadProcessId( hWnd, 0 );
  6. const DWORD thisThreadId = AfxGetThread()->m_nThreadID;
  7. // const bool bGuiThread = IsGUIThread(FALSE);
  8. // ctrlTree.InvokeRequired == false // this might work in .NET but not MFC.
  9.  
  10. bool isGuiThread =
  11. pWnd != NULL &&
  12. hWnd != NULL &&
  13. thisThreadId == WindowThreadId;
  14.  
  15. ASSERT( isGuiThread );
  16.  
  17. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: jimfred on March 11, 2009

I never got this to work reliably and instead used my own variable to store the thread ID for the thread that created a window. This was a concern for multi-thread MFC apps.

You need to login to post a comment.