User Interface Threads in MFC

A thread is the path of execution of a process. A thread by itself, uses only a part of the memory and address space of the process in which it is getting executed. MFC provides two kinds of threads viz.Worker threads and User Interface threads. The differences between a user interface thread in MFC and worker thread are

  • User Interface thread has its own message pump. It can implement message maps and message handlers. Worker thread does not have its own message pump.
  • A worker thread often terminates once its work is done. On other hand a UI-Thread often remains in the memory standby (inside Run() method, which does message pumping) to do the work on receiving any message.

This article explains how to create and use User interface threads in MFC.

Step 1:
Create a project called MyThreadProject with Single Document Interface. Open class wizard by clicking Menu –> View –>ClassWizard or by pressing the hot key combination Ctrl + W. Click on AddClass–>NewClass. Derive a class named “MyThread” from CWinThread. The ClassWizard will create the necessary files (MyThread.h & MyThread.cpp) and add them to the workspace. This will form the basis for creating our user interface thread.

Step 2:
Open the stdafx.h file and add the following line to it.

#define WM_MYTHREADMESSAGE (WM_USER+1)

Step 3:
Open the MyThread.CPP file. Inside the message map of the MyThread.cpp file, add the message handler declaration as follows.

//MyThread.cpp
BEGIN_MESSAGE_MAP(CMyThread,
CWinThread)
//{{AFX_MSG_MAP(CMyThread)
// NOTE - the ClassWizard will add
and remove mapping macros here.

ON_THREAD_MESSAGE ( WM_MYTHREADMESSAGE, MyMessageHandler )
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

Step 4:
Add a declaration for the function MyMessageHandler in the MyThread.h as follows.

//MyThread.h
class CMyThread : public CWinThread
{

DECLARE_DYNCREATE(CMyThread)
protected:
// Attributes
public:
CMyThread();
void
MyMessageHandler(WPARAM, LPARAM);
...
};
Add a definition for the function
MyMessageHandler in the MyThread.cpp file
//MyThread.cpp
void MyThread::MyMessageHandler(WPARAM, LPARAM)
{
//required
functionality.
}

Pls do not forget to change the access level of the CMyThread() constructor and its corresponding destructor from protected to public. The default created by the ClassWizard will be with protected level of access.

Now we are ready to call this thread and handler for creating a user interface thread in mfc.

Step 5:
Add the header file to the class where we want to use it. Let us assume that we want to use it in a CView derived class called CMyThreadProjectView. The following line should go into the MyThreadProjectView.cpp file.

#include "MyThread.h"

Step 6:
Declare a variable for the thread class at the required scope level for MyThreadProjectView.cpp file. Depending on the requirements of creating a dynamic user interface thread, it can be global, local or member of a class also. Then call CreateThread.

//MyThreadProjectView.cpp
MyThread* pThread;
pThread = new MyThread();
pThread->CreateThread();
pThread->PostThreadMessage(WM_MYTHREADMESSAGE,NULL,NULL);

Sample Code
Please find the Sample code here.