CWinThread in MFC

 

 

First introduce two kinds of thread:

worker thread : general thread, have no message pump, should do the real time costing works.

user-interface thread: a thread of message peeking is running. Only deal with messages.

 

UI thread

The first thing you must do when creating a user-interface thread is derive a class from CWinThread. You must declare and implement this class, using the DECLARE_DYNCREATE and IMPLEMENT_DYNCREATE macros. This class must override some functions and can override others. These functions and what they should do are presented in the following table.

Function

Purpose

ExitInstance

Perform cleanup when thread terminates. Usually overridden.

InitInstance

Perform thread instance initialization. Must be overridden. And if return FALSE the thread will ends by itself.

OnIdle

Perform thread-specific idle-time processing. Not usually overridden.

PreTranslateMessage

Filter messages before they are dispatched to TranslateMessage and DispatchMessage. Not usually overridden.

ProcessWndProcException

Intercept unhandled exceptions thrown by the thread's message and command handlers. Not usually overridden.

Run

Controlling function for the thread. Contains the message pump. Rarely overridden.


W orker thread

Only two steps are required to get your thread running: implementing the controlling function and starting the thread. It is not necessary to derive a class from CWinThread.

 

AfxBeginThread for worker thread

To begin execution of your worker thread, call AfxBeginThread, providing the following information:

l         The address of the controlling function.

l         The parameter to be passed to the controlling function.

l         (Optional) The desired priority of the thread. The default is normal priority. For more information about the available priority levels, see SetThreadPriority in the Platform SDK.

l         (Optional) The desired stack size for the thread. The default is the same size stack as the creating thread.

l         (Optional) CREATE_SUSPENDED if you want the thread to be created in a suspended state. The default is 0, or start the thread normally.

l         (Optional) The desired security attributes. The default is the same access as the parent thread. For more information about the format of this security information, see SECURITY_ATTRIBUTES in the Platform SDK.

Implementing the Controlling Function

When this function is entered, the thread starts, and when it exits, the thread terminates. This function should have the following prototype:

UINT MyControllingFunction( LPVOID pParam );

When the function terminates, it should return a UINT value indicating the reason for termination.

 

Controlling Function Example

 

Start and stop

AfxBeginThread creates a new CWinThread object, calls its CreateThread function to start executing the thread, and returns a pointer to the thread. Instead of calling AfxBeginThread, you can construct a CWinThread-derived object and then call CreateThread.

To end the thread, call AfxEndThread from within the thread, or return from the controlling function of the worker thread.

Can not delete the CreateThread cause the destructor is protected member function and used by dynamic creation.

 

Normal Thread Termination

For a worker thread, use either the AfxEndThread function or a return statement. The return value signifies the reason for termination. Typically, 0 signifies successful completion

For a user-interface thread, from within the user-interface thread, call PostQuitMessage in the Platform SDK.

 

 

Premature Thread Termination

Call AfxEndThread from within the thread. Pass the desired exit code as the only parameter. This stops execution of the thread, deallocates the thread's stack, detaches all DLLs attached to the thread, and deletes the thread object from memory.

AfxEndThread must be called from within the thread to be terminated. If you want to terminate a thread from another thread, you must set up a communication method between the two threads.

 

Note:

MFC objects are not thread-safe at the object level, only at the class level. This means that you can have two separate threads manipulating two different CString objects, but not two threads manipulating the same CString object.

 

The CWinThread class is necessary to make your code and MFC fully thread-safe. Thread-local data used by the framework to maintain thread-specific information is managed by CWinThread objects. Because of this dependence on CWinThread to handle thread-local data, any thread that uses MFC must be created by MFC . For example, a thread created by the run-time function _beginthread, _beginthreadex cannot use any MFC APIs.

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值