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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值