MFC多线程之工作者线程

1、关于工作者线程和用户界面线程的区别不再累述,主要是有无消息循环的区别。


2、下面通过一个基于对话框的工程来实现一个工作者线程。


3、AfxBeginThread函数(MSDN):

AfxBeginThread

This function creates a new thread. The first form of AfxBeginThread creates a worker thread. The second form creates a user-interface thread.

AfxBeginThread creates a new CWinThread object, calls its CreateThread function to start executing the thread, and returns a pointer to the thread. Checks are made throughout the procedure to make sure all objects are deallocated properly should any part of the creation fail. To end the thread, call AfxEndThread from within the thread, or return from the controlling function of the worker thread.

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 );
Parameters
pfnThreadProc
Points to the controlling function for the worker thread. Cannot be NULL. This function must be declared as follows:
UINT MyControllingFunction( LPVOID pParam );
pThreadClass
The RUNTIME_CLASS of an object derived from CWinThread.
pParam
Parameter to be passed to the controlling function as shown in the parameter to the function declaration in pfnThreadProc.
nPriority
The desired priority of the thread. If 0, the same priority as the creating thread will be used. For a full list and description of the available priorities, see SetThreadPriority.
nStackSize
Specifies the size in bytes of the stack for the new thread. If 0, the stack size defaults to the same size stack as the creating thread.
dwCreateFlags
Specifies an additional flag that controls the creation of the thread. This flag can contain one of two values:
  • CREATE_SUSPENDED   Start the thread with a suspend count of one. Use CREATE_SUSPENDED if you want to initialize any member data of the CWinThread object, such as m_bAutoDelete or any members of your derived class, before the thread starts running. Once your initialization is complete, use the CWinThread::ResumeThread to start the thread running. The thread will not execute until CWinThread::ResumeThread is called.
  • 0   Start the thread immediately after creation.
lpSecurityAttrs
Points to a SECURITY_ATTRIBUTES structure that specifies the security attributes for the thread. If NULL, the same security attributes as the creating thread will be used. For more information on this structure, see the Platform SDK.
Return Value

Pointer to the newly created thread object.

Requirements

  Windows CE versions: 2.0 and later
  Header file: Declared in Afxwin.h
  Platform: H/PC Pro, Palms-size PC, Pocket PC



4、在对话框上添加一个按钮,双击添加响应函数:

void CMFC_ThreadDlg::OnBnClickedBtnWorker()
{
	// TODO: 在此添加控件通知处理程序代码
	AfxBeginThread(workerThreadFunc, NULL);
}

5、实现workerThreadFunc函数:

(1)全局函数:

UINT workerThreadFunc(LPVOID pParam);

UINT workerThreadFunc(LPVOID pParam)
{
	AfxMessageBox(_T("New Worker Thread!"));
	return 1;
}

(2)静态的成员函数:

static UINT workerThreadFunc(LPVOID pParam);
UINT CMFC_ThreadDlg::workerThreadFunc(LPVOID pParam)
{
	AfxMessageBox(_T("New Worker Thread!"));
	return 1;
}

6、运行后点击按钮弹处一个对话框,显示New Worker Thread!


7、OK,这样就创建了一个简单的线程了。


8、线程的挂起:CREATE_SUSPENDED


9、修改函数参数:

void CMFC_ThreadDlg::OnBnClickedBtnWorker()
{
	// TODO: 在此添加控件通知处理程序代码
	AfxBeginThread(workerThreadFunc, NULL, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
}

此时,运行程序单击按钮,会发现“New Worker Thread!”对话框没有出来,因为这个线程被挂起了。


10、添加一个成员变量,用于保存新建线程的句柄,根据这个句柄可以使挂起线程继续运行:

CWinThread *pThread;
将其初始化为NULL:

CMFC_ThreadDlg::CMFC_ThreadDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CMFC_ThreadDlg::IDD, pParent)
	, pThread(NULL)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
保存线程句柄:

void CMFC_ThreadDlg::OnBnClickedBtnWorker()
{
	// TODO: 在此添加控件通知处理程序代码
	pThread = AfxBeginThread(workerThreadFunc, NULL, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
}

11、添加一个按钮,用于继续运行挂起的线程:

void CMFC_ThreadDlg::OnBnClickedBtnResumethread()
{
	// TODO: 在此添加控件通知处理程序代码
	if (pThread != NULL)
	{
		pThread->ResumeThread();
	}
}

12、此时,依次点击两个按钮,依旧会弹出“New Worker Thread!”对话框。


13、当然也可以根据线程句柄再次挂起线程:

<span style="font-size:18px;">void CMFC_ThreadDlg::OnBnClickedBtnSuspendthread()
{
	// TODO: 在此添加控件通知处理程序代码
	if (pThread != NULL)
	{
		pThread->SuspendThread();
	}
}</span>


14、退出线程函数:AfxEndThread() 或 ExitThread(0),必须在线程函数中调用:

AfxEndThread

This function terminates the currently executing thread. Must be called from within the thread to be terminated.

void AFXAPI AfxEndThread(
UINT nExitCode,
BOOL bDelete = TRUE );
Parameters
nExitCode
Specifies the exit code of the thread.
bDelete
Deletes the thread object from memory. 

15、调用如下:

UINT CMFC_ThreadDlg::workerThreadFunc(LPVOID pParam)
{
	AfxMessageBox(_T("New Worker Thread!"));
	AfxEndThread(0);
	return 1;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值