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

Threads And Timers

Rate me:
Please Sign up or sign in to vote.
4.60/5 (11 votes)
26 Jan 20041 min read 162.8K   4.2K   32  
Adding a WM_TIMER message to CWinThreads.
// MFCThreadTimerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFCThreadTimer.h"
#include "MFCThreadTimerDlg.h"
#include ".\mfcthreadtimerdlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CMFCThreadTimerDlg dialog
BEGIN_DHTML_EVENT_MAP(CMFCThreadTimerDlg)
	DHTML_EVENT_ONCLICK(_T("ButtonOK"),				OnButtonOK)
	DHTML_EVENT_ONCLICK(_T("ButtonCancel"),			OnButtonCancel)
END_DHTML_EVENT_MAP()


CMFCThreadTimerDlg::CMFCThreadTimerDlg(CWnd* pParent /*=NULL*/)
	: CDHtmlDialog(CMFCThreadTimerDlg::IDD, CMFCThreadTimerDlg::IDH, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFCThreadTimerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDHtmlDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_NUMBEROFTHREADS, m_SliderNumberofThreads);
	DDX_Control(pDX, IDC_LIST2, m_LogList);
}

BEGIN_MESSAGE_MAP(CMFCThreadTimerDlg, CDHtmlDialog)
	ON_WM_SYSCOMMAND()
	ON_MESSAGE(LOGMSG,								OnLogMessage)
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_STARTTHREAD,					OnStartThreads)
	ON_BN_CLICKED(IDC_STOPTHREAD,					OnStopThreads)
END_MESSAGE_MAP()


// CMFCThreadTimerDlg message handlers

BOOL CMFCThreadTimerDlg::OnInitDialog()
{
	CDHtmlDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	m_SliderNumberofThreads.SetRange(1, 100, true);
	m_SliderNumberofThreads.SetTicFreq( 10 );
	UpdateData(TRUE);
	//
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMFCThreadTimerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDHtmlDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMFCThreadTimerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDHtmlDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMFCThreadTimerDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}
HRESULT CMFCThreadTimerDlg::OnButtonOK(IHTMLElement* /*pElement*/)
{
	OnOK();
	return S_OK;
}

HRESULT CMFCThreadTimerDlg::OnButtonCancel(IHTMLElement* /*pElement*/)
{
	OnCancel();
	return S_OK;
}

void CMFCThreadTimerDlg::OnStartThreads()
{
	CMyThread *pThread;
	int i;
	int numthreads = m_SliderNumberofThreads.GetPos();

	for (i=0; i < numthreads; i++)
	{
		pThread = (CMyThread*) AfxBeginThread(	RUNTIME_CLASS(CMyThread),
												THREAD_PRIORITY_NORMAL,
												NULL,
												CREATE_SUSPENDED, 
												NULL);
		if (pThread)
		{
			m_ThreadList.Add(pThread);
			pThread->SetLogWindow(this);
			pThread->ResumeThread();
		}
	}
}

void CMFCThreadTimerDlg::OnStopThreads()
{
	int i;

	// send a quit message to all threads
	for (i=0; i <= m_ThreadList.GetUpperBound(); i++)
	{
		// tell the thread to quit
		m_ThreadList[i]->PostThreadMessage(WM_QUIT, NULL, NULL);
	}
	// remove the thread objects from the list
	m_ThreadList.RemoveAll();
	//
}
 LRESULT CMFCThreadTimerDlg::OnLogMessage(WPARAM wparam, LPARAM lparam)
 {
	 CString* pLogMessage = (CString*) wparam;

	 m_LogList.AddString( *pLogMessage);

     return UpdateData(FALSE);
 }

 void CMFCThreadTimerDlg::OnBnClickedStartthread()
 {
	 // TODO: Add your control notification handler code here
 }

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
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