Click here to Skip to main content
15,892,746 members
Articles / Desktop Programming / MFC

The Practical Guide to Multithreading - Part 1

Rate me:
Please Sign up or sign in to vote.
4.89/5 (123 votes)
6 Apr 2010CPOL17 min read 304.4K   10.4K   453  
Learn from this guide how and when - as well as when not - to use multithreading.
// Example2_Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "Example2_Dlg.h"


#define WM_MY_THREAD_MESSAGE	WM_APP+100

// CExample2_Dlg dialog

IMPLEMENT_DYNAMIC(CExample2_Dlg, CDialog)

CExample2_Dlg::CExample2_Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CExample2_Dlg::IDD, pParent),

CancelEvent(FALSE, TRUE)	// Non-signaled, manual-reset
{

}

CExample2_Dlg::~CExample2_Dlg()
{
}

void CExample2_Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_START, StartBox);
	DDX_Control(pDX, IDC_END, EndBox);
	DDX_Control(pDX, IDC_PROGRESS, ProgressBar);
	DDX_Control(pDX, IDC_STATUS, StatusText);
}


BEGIN_MESSAGE_MAP(CExample2_Dlg, CDialog)
	ON_BN_CLICKED(ID_CANCEL, &CExample2_Dlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDOK, &CExample2_Dlg::OnBnClickedOk)

	ON_MESSAGE(WM_MY_THREAD_MESSAGE, OnThreadMessage)

END_MESSAGE_MAP()


LRESULT CExample2_Dlg::OnThreadMessage(WPARAM wParam, LPARAM)
{
	int nProgress= (int)wParam;

	// update progress bar
	ProgressBar.SetPos(nProgress);

	CString strStatus;
	strStatus.Format("Processing item: %d", nProgress);

	// update status text
	StatusText.SetWindowText(strStatus);

	return 0;
}

void CExample2_Dlg::OnBnClickedCancel()
{
	GetDlgItem(ID_CANCEL)->EnableWindow(FALSE);

	// Let thread know about this event
	CancelEvent.SetEvent();

	// Wait till target thread responds, and exists
	WaitForSingleObject(ptrThread->m_hThread,INFINITE);

	// update status text
	StatusText.SetWindowText("Canceled!");
}

static UINT Example2Thread(void*);

void CExample2_Dlg::OnBnClickedOk()
{
	// Get start and end values
	StartFrom = GetDlgItemInt(IDC_START);
	EndTo = GetDlgItemInt(IDC_END);

	// Set progress bar range
	ProgressBar.SetRange32(StartFrom, EndTo);

	// Disable button
	GetDlgItem(IDOK)->EnableWindow(FALSE);
	GetDlgItem(ID_CANCEL)->EnableWindow(TRUE);

	// Start off the thread
	ptrThread = AfxBeginThread(Example2Thread, this);
}

UINT Example2Thread(void *pParam)
{
	CExample2_Dlg* pThis= (CExample2_Dlg*)pParam;	

	for (int nValue = pThis->StartFrom; nValue <= pThis->EndTo;  ++nValue)
	{
		if( pThis->CancelEvent.Lock(0) )
			return -1;

		pThis->PostMessage(WM_MY_THREAD_MESSAGE, nValue);	

		Sleep(200);
	}

	return 0;
}

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
Software Developer (Senior)
India India
Started programming with GwBasic back in 1996 (Those lovely days!). Found the hidden talent!

Touched COBOL and Quick Basic for a while.

Finally learned C and C++ entirely on my own, and fell in love with C++, still in love! Began with Turbo C 2.0/3.0, then to VC6 for 4 years! Finally on VC2008/2010.

I enjoy programming, mostly the system programming, but the UI is always on top of MFC! Quite experienced on other environments and platforms, but I prefer Visual C++. Zeal to learn, and to share!

Comments and Discussions