多线程学习4——使用用户界面线程

本文介绍了如何创建用户界面线程,需要从CWinThread派生一个类并重载InitInstance等成员函数。AfxBeginThread函数用于启动线程,使用CRuntimeClass指向派生类对象。线程的运行和控制可通过CWinThread的成员函数如SuspendThread和ResumeThread进行管理。
摘要由CSDN通过智能技术生成

 使用用户界面线程

  创建一个用户界面线程比一个工作者线程复杂的多:前者必须要从CWinThread派生一个类,并重载一些成员函数。调用AfxBeginThread开始一个用户界面线程与开始一个工作者线程很相似,只是工作者线程的控制函数和参数被指向CWinThread导出类的对象的指针多代替。此时,AfxBeginThread调用之后也不再运行控制函数,而是调用线程对性的InitInstance函数。

 

  具体步骤:

(1)从CWinThread派生一个线程类,重载成员函数:

  • InitInstanc 必须重载。而且在类的声明和实现中必须使用DECLARE_DYNCREATE和IMPLEMENT_DYNCREATE宏。
  • CWinThread的其他几个函数可以根据需要选择是否重载。
  • 在CWinThread的Run成员函数中,会获取线程的窗口消息,它将这些消息送给PreTranslateMessage处理。可以重载这个函数,将在这些消息通过MFC的标准消息路线发送。
  • CWinThread的数据成员,包含:线程ID、线程句柄以及指向程序的主窗口的指针。其中M_bAtoDelete成员决定线程结束时对象是否被删除。
  • 利用CWinThread的SuspendThread和ResumeThread函数可以从外部控制线程。SuspendThread使线程的挂起次数增加,而ResumeThread则使之减少。如果线程的挂起次数不为0,操作系统将不对其进行调度。二者应该配对使用。

CWinThread Members

Base Class Members

CObject Members

CCmdTarget Members

Data Members

m_bAutoDeleteSpecifies whether to destroy the object at thread termination.
m_hThreadHandle to the current thread.
m_nThreadIDID of the current thread.
m_pActiveWndPointer to the main window of the container application when an OLE server is in-place active.
m_pMainWndHolds a pointer to the application's main window.

Construction

CreateThreadStarts execution of a CWinThread object.
CWinThreadConstructs a CWinThread object.

Operations

GetMainWndRetrieves a pointer to the main window for the thread.
GetThreadPriorityGets the priority of the current thread.
PostThreadMessagePosts a message to another CWinThread object.
ResumeThreadDecrements a thread's suspend count.
SetThreadPrioritySets the priority of the current thread.
SuspendThreadIncrements a thread's suspend count.

Overridables

ExitInstanceOverride to clean up when your thread terminates.
InitInstanceOverride to perform thread instance initialization.
IsIdleMessageChecks for special messages.
OnIdleOverride to perform thread-specific idle-time processing.
PreTranslateMessageFilters messages before they are dispatched to the Windows functions TranslateMessage and DispatchMessage.
ProcessMessageFilterIntercepts certain messages before they reach the application.
ProcessWndProcExceptionIntercepts all unhandled exceptions thrown by the thread's message and command handlers.
PumpMessageContains the thread's message loop.
RunControlling function for threads with a message pump. Override to customize the default message loop.

Operators

operator HANDLE

Retrieves the handle of the CWinThread object.

 

(2)调用AfxBeginThread函数:

CWinThread *AfxBeginThread(

     CRuntimeClass* pThreadClass,

     int nPriority = THREAD_PRIORITY_NORMAL,

     UINT nStackSize = 0,

     DWORD dwCreateFlags = 0,

     LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL

);

 

注:pThreadClass它指向一个CRuntimeClass对象,该对象由RUNTIME_CLASS宏从CWinThread的派生类创建。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值