Click here to Skip to main content
15,895,709 members
Articles / Desktop Programming / MFC

Asynchronous Function Calls: Working with Win32 threads made easy

Rate me:
Please Sign up or sign in to vote.
4.20/5 (16 votes)
7 May 2007CPOL9 min read 37.7K   613   21  
An article on the new approach to utilize Win32 threads in a more intuitive manner.
// ExampleDlg4.cpp : implementation file
//

#include "stdafx.h"
#include "ExampleDlg4.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CExampleDlg4 dialog


CExampleDlg4::CExampleDlg4(CWnd* pParent /*=NULL*/)
  : CResizableDialog(CExampleDlg4::IDD, pParent)
{
  //{{AFX_DATA_INIT(CExampleDlg4)
    // NOTE: the ClassWizard will add member initialization here
  //}}AFX_DATA_INIT
}


void CExampleDlg4::DoDataExchange(CDataExchange* pDX)
{
  CResizableDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CExampleDlg4)
    // NOTE: the ClassWizard will add DDX and DDV calls here
  //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CExampleDlg4, CResizableDialog)
  //{{AFX_MSG_MAP(CExampleDlg4)
  ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExampleDlg4 message handlers

BOOL CExampleDlg4::OnInitDialog()
{
  CResizableDialog::OnInitDialog();

  // Resizable dialog code
  ShowSizeGrip( FALSE );


  return TRUE;  // return TRUE unless you set the focus to a control
                // EXCEPTION: OCX Property Pages should return FALSE
}

void CExampleDlg4::OnOK()
{
  // TODO: Add extra validation here

  //  CResizableDialog::OnOK();
}

void CExampleDlg4::OnCancel()
{
  // TODO: Add extra cleanup here

  //  CResizableDialog::OnCancel();
}

void CExampleDlg4::OnClose()
{
  // TODO: Add your message handler code here and/or call default

  //  CResizableDialog::OnClose();
  CResizableDialog::OnOK();
}

void CExampleDlg4::OnButton1() 
{
	// TODO: Add your control notification handler code here
  CString strCaption;
  strCaption.Format(_T("Thread ID: %x"), ::GetCurrentThreadId() );

  afc::proxy p = afc::launch<afc::mfc_thread<> >(
    boost::bind( &CExampleDlg4::MyFunction1, this ) );

  afc::thread_collector::contract( p );

  DWORD res = p.wait_with_message_loop( 5000 );

  if( WAIT_OBJECT_0 == res )
  {
    ::MessageBox( m_hWnd, _T("WAIT_OBJECT_0"), strCaption, MB_OK );
  }
  else if( WAIT_TIMEOUT == res )
  {
    ::MessageBox( m_hWnd, _T("WAIT_TIMEOUT"), strCaption, MB_OK );
  }
	
}

void CExampleDlg4::MyFunction1()
{
  CString strCaption;
  strCaption.Format(_T("Thread ID: %x"), ::GetCurrentThreadId() );

  ::MessageBox( m_hWnd, _T("Ready to sleep!"), strCaption, MB_OK );

  ::Sleep( 10000 );

  ::MessageBox( m_hWnd, _T("::Sleep( 10000 ) is completed."), strCaption, MB_OK );
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions