在MFC中,我们可以用到的一个API是AfxBeginThread,它的函数声明原型如下:
CWinThread* AfxBeginThread(
AFX_THREADPROC pfnThreadProc,
LPVOID pParam,
int nPriority = THREAD_PRIORITY_NORMAL,
UINT nStackSize = 0,
DWORD dwCreateFlags = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
);
CWinThread* AfxBeginThread(
CRuntimeClass* pThreadClass,
int nPriority = THREAD_PRIORITY_NORMAL,
UINT nStackSize = 0,
DWORD dwCreateFlags = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
);
这两个函数中,第一个可以创建一个用户工作线程,第二个可以创建一个用户界面线程。参数说明如下:
-
pfnThreadProc: 指向工作线程的函数地址,函数格式为UINT __cdecl MyControllingFunction( LPVOID pParam );
-
-
pThreadClass: 用户界面线程类,一般是从CWinThread继承而来
-
-
pParam: 需要传给线程的参数
-
-
nPriority: 优先级属性标识符,详见SetThreadPriority 中对其的描述,一般可设为NULL
-
-
nStackSize: 线程栈大小,如果设置为0的话,就和创建线程栈大小一样
-
-
dwCreateFlags: 创建标识符,如果设为
CREATE_SUSPENDED ,线程在创建之后会挂起,期间你可以初始化一些变量之类的东西,它会等待直到你调用了
CWinThread::ResumeThread 后才继续执行。如果设置为
0 则线程创建之后立即执行。
-
-
lpSecurityAttrs: 安全属性标识符,可参看SECURITY_ATTRIBUTES 结构体实例化它,一般情况下可以设置为NULL,表示使用和创建线程相同的安全属性。
-
-
由MSDN可以看到,这个函数仍然也是调用了CreateThread函数来创建线程的,它与CreateThread不同的地方是它也如C++运行库中的_beginthread一样,需要做一些初始化工作,所以,在MFC的程序中,他的操作性要比CreateThread容易一些,不容易出错。
-
-
对于线程的终止和返回,可以参看 多线程编程(一)——CreateThread 和MSDN中的说明,此处不在做过多解释。
-
下面举例说明用法:
UserInterfaceThread.h
view plain copy to clipboard print ?
- #pragma once
- #include "afxwin.h"
- class CUserInterfaceThread : public CWinThread
- {
- DECLARE_DYNCREATE(CUserInterfaceThread)
- public:
- CUserInterfaceThread(void);
- ~CUserInterfaceThread(void);
- virtual BOOL InitInstance();
- virtual int ExitInstance();
- };
#pragma once #include "afxwin.h" class CUserInterfaceThread : public CWinThread { DECLARE_DYNCREATE(CUserInterfaceThread) public: CUserInterfaceThread(void); ~CUserInterfaceThread(void); virtual BOOL InitInstance(); virtual int ExitInstance(); };
UserInterfaceThread.cpp
view plain copy to clipboard print ?
- #include "UserInterfaceThread.h"
- IMPLEMENT_DYNCREATE(CUserInterfaceThread, CWinThread)
- CUserInterfaceThread::CUserInterfaceThread(void)
- {
- }
- CUserInterfaceThread::~CUserInterfaceThread(void)
- {
- }
- BOOL CUserInterfaceThread::InitInstance()
- {
-
- MessageBox(NULL, _T("你好,这是用户界面线程!"), _T("欢迎"), MB_OK);
- return CWinThread::InitInstance();
- }
- int CUserInterfaceThread::ExitInstance()
- {
-
- return CWinThread::ExitInstance();
- }
#include "UserInterfaceThread.h" IMPLEMENT_DYNCREATE(CUserInterfaceThread, CWinThread) CUserInterfaceThread::CUserInterfaceThread(void) { } CUserInterfaceThread::~CUserInterfaceThread(void) { } BOOL CUserInterfaceThread::InitInstance() { // TODO: 在此添加专用代码和/或调用基类 MessageBox(NULL, _T("你好,这是用户界面线程!"), _T("欢迎"), MB_OK); return CWinThread::InitInstance(); } int CUserInterfaceThread::ExitInstance() { // TODO: 在此添加专用代码和/或调用基类 return CWinThread::ExitInstance(); }
AFXThreading.h
view plain copy to clipboard print ?
- #ifndef MULTITHREAD
- #define MULTITHREAD
- class DrawPicture
- {
- public:
- static UINT MyThreadFunc(LPVOID lpParam );
-
- void Draw();
- private:
- static int nPoint;
- };
- #endif
#ifndef MULTITHREAD #define MULTITHREAD class DrawPicture { public: static UINT MyThreadFunc(LPVOID lpParam ); void Draw(); private: static int nPoint; }; #endif
AFXThreading.cpp
view plain copy to clipboard print ?
- #include <iostream>
- #include <afxwin.h>
- #include "AFXThreading.h"
- #include "UserInterfaceThread.h"
- using namespace std;
- int DrawPicture::nPoint = 20;
- UINT DrawPicture::MyThreadFunc( LPVOID lpParam )
- {
- while(nPoint > 0)
- {
- cout<<"It is Point:"<<nPoint--<<endl;
- }
-
- return 0;
- }
- void DrawPicture::Draw()
- {
- #pragma region 创建用户工作线程
- AfxBeginThread(MyThreadFunc, 0, 0, 0, NULL);
- #pragma endregion 创建用户工作线程
- #pragma region 创建用户界面线程
- CUserInterfaceThread *pInterfaceThread = (CUserInterfaceThread*)AfxBeginThread(
- RUNTIME_CLASS(CUserInterfaceThread),
- THREAD_PRIORITY_NORMAL,
- 0,
- CREATE_SUSPENDED,
- NULL
- );
-
- if (NULL != pInterfaceThread)
- {
- pInterfaceThread->ResumeThread();
- }
- #pragma endregion 创建用户界面线程
- }
- int main(void)
- {
- DrawPicture *pDraw = new DrawPicture;
- pDraw->Draw();
- delete pDraw;
- system("pause");
- return 0;
- }
#include <iostream> #include <afxwin.h> #include "AFXThreading.h" #include "UserInterfaceThread.h" using namespace std; int DrawPicture::nPoint = 20; UINT DrawPicture::MyThreadFunc( LPVOID lpParam ) { while(nPoint > 0) { cout<<"It is Point:"<<nPoint--<<endl; } //AfxEndThread(0); //尽量避免使用,让线程自动正常退出 return 0; } void DrawPicture::Draw() { #pragma region 创建用户工作线程 AfxBeginThread(MyThreadFunc, 0, 0, 0, NULL); #pragma endregion 创建用户工作线程 #pragma region 创建用户界面线程 CUserInterfaceThread *pInterfaceThread = (CUserInterfaceThread*)AfxBeginThread( RUNTIME_CLASS(CUserInterfaceThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL ); if (NULL != pInterfaceThread) { pInterfaceThread->ResumeThread();//恢复线程 } #pragma endregion 创建用户界面线程 } int main(void) { DrawPicture *pDraw = new DrawPicture; pDraw->Draw(); delete pDraw; system("pause"); return 0; }
应当注意的是,在创建用户界面线程的时候,必须首先从 CWinThread 派生类。必须使用 DECLARE_DYNCREATE 和 IMPLEMENT_DYNCREATE 宏声明并实现此类。此类必须重写某些函数(InitInstance ),也可以重写其他函数(ExitInstance )。详情参看MSDN中《多线程处理:创建用户界面线程》一文。
本系列示例代码下载