+ Reply to Thread
Results 1 to 2 of 2

Thread: wait for a thread to complete under MFC/WIN32

  1. #1

    wait for a thread to complete under MFC/WIN32

    I have a thread and I want to wait for it for some time. How can I do this either in MFC/WIN32. Please give me some good and basic example code. Thanks.

  2. #2
    Senior Member
    Join Date
    Feb 2012
    Posts
    109
    Whenever you create a thread, you will get thread handle. You can then wait to thread on thread handle using WaitForSingleObject API.

    Example:

    Code:
    UINT WINAPI ThreadFunc(void *pParam)
    {
    	Sleep(500) ;
    	return 0 ;
    }
    
    BOOL CTestMFCCodeExpertsDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    	CWinThread *pThread = AfxBeginThread((AFX_THREADPROC) ThreadFunc, NULL, 0, 0, 0, NULL) ;
    	if (pThread != NULL)
    	{
    		WaitForSingleObject(pThread->m_hThread, 1000) ;//wait for a thread for 1 second.
    	}
    	return TRUE;
    }
    Enjoy!

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts