您的位置:首页 > 其它

AfxBeginThread创建用户界面线程

2016-01-26 20:26 447 查看
<span style="font-family:SimSun;font-size:18px;">m_threadManager = (CManagerThread *)AfxBeginThread(
RUNTIME_CLASS(CManagerThread),
THREAD_PRIORITY_NORMAL,10240,NULL);</span></span>
<span style="font-family:SimSun;font-size:18px;">1、创建用户界面线程时,必须首先从CWinThread派生类,即CManagerThread类派生自CWinThread类。</span></span>
<span style="font-family:SimSun;font-size:18px;">2、必须使用DECLARE_DYNCREATE和IMPLEMENT_DYNCREATE宏声明并实现此类。</span></span>
<span style="font-family:SimSun;font-size:18px;">在类的声明中(ManagerThread.h):</span></span>
<span style="font-family:SimSun;font-size:18px;">class CManagerThread : public CWinThread
{
DECLARE_DYNCREATE(CManagerThread)
protected:
CManagerThread();
protected:
virtual ~CManagerThread();
}
</span></span>
在类的实现中(ManagerThread.cpp):

<span style="font-family:SimSun;font-size:18px;">IMPLEMENT_DYNCREATE(CManagerThread, CWinThread)</span></span>



<span style="font-family:SimSun;font-size:18px;">3、此类必须重写InitInstance和ExitInstance两个函数。
以下来源于:<a target=_blank href="http://blog.csdn.net/akof1314/article/details/5762027">点击打开链接</a>  无幻	</span></span>
<span style="font-family:SimSun;font-size:18px;"></span><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">用户界面线程在运行时会有一个窗口界面和与其相对应的窗口函数,所以它可以通过响应消息来和用户进行交互。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;"><span style="color: rgb(255, 102, 0);">AfxBeginThread</span> 函数原型如下:</p><div style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px; border: 1px dashed rgb(204, 204, 204); background-color: rgb(239, 239, 239);">CWinThread *AfxBeginThread( 
    CRuntimeClass *pThreadClass,         <span style="color: rgb(0, 128, 0);">//从CWinThread派生的RUNTIME_CLASS类 </span> 
    <span style="color: rgb(0, 0, 255);">int</span>  nPriority <span style="color: rgb(0, 128, 0);">/* = THREAD_PRIORITY_NORMAL */</span> ,      <span style="color: rgb(0, 128, 0);">//指定线程的优先级 </span> 
    UINT nStackSize <span style="color: rgb(0, 128, 0);">/* = 0 */</span> ,                <span style="color: rgb(0, 128, 0);">//定义新线程的堆栈大小 </span> 
    DWORD dwCreateFlags <span style="color: rgb(0, 128, 0);">/* = 0 */</span> ,     <span style="color: rgb(0, 128, 0);">//为控制线程创建的附加标志 </span> 
    LPSECURITY_ATTRIBUTES lpSecurityAttrs <span style="color: rgb(0, 128, 0);">/* = NULL */</span>     <span style="color: rgb(0, 128, 0);">//指定线程的安全属性 </span> 
) 
</div><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">用户界面线程必须包含有消息循环,以便可以处理用户消息。要使用用户界面线程,必须派生自<span style="color: rgb(255, 102, 0);">CWinThread</span> 类一个线程类,而且一般要重写类的<span style="color: rgb(255, 102, 0);">InitInstance</span> ()和<span style="color: rgb(255, 102, 0);">ExitInstance</span> ()函数。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">示例:编写一个应用程序,当用户在程序主窗口按下鼠标左键时,会启动一个用户界面线程。当用户在线程窗口界面按下鼠标左键时,会弹出一个信息框。
1.新建单文档程序;
2.单击菜单栏“项目”→“添加类”→“MFC类”,基类选择“<span style="color: rgb(255, 102, 0);">CWinThread</span> ",类名输入CMyThread,点”完成“,同时以类似步骤以CFrameWnd类为基类派生<span style="color: rgb(255, 102, 0);">CMyWnd</span> 类;
3.在视图类实现文件包含头文件:</p><div style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px; border: 1px dashed rgb(204, 204, 204); background-color: rgb(239, 239, 239);"><span style="color: rgb(0, 0, 255);">#include</span>  <span style="color: rgb(163, 21, 21);">"MyThread.h"</span> 
</div><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">4.在CMyThread类实现文件中包含头文件:</p><div style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px; border: 1px dashed rgb(204, 204, 204); background-color: rgb(239, 239, 239);"><span style="color: rgb(0, 0, 255);">#include</span>  <span style="color: rgb(163, 21, 21);">"MyWnd.h"</span> 
</div><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">5.在CMyThread::InitInstance()中创建线程中的窗体:</p><div style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px; border: 1px dashed rgb(204, 204, 204); background-color: rgb(239, 239, 239);">BOOL CMyThread::InitInstance() 
{ 
    CMyWnd *pFrameWnd = <span style="color: rgb(0, 0, 255);">new</span>  CMyWnd(); 
    pFrameWnd->Create(NULL, _T(<span style="color: rgb(163, 21, 21);">"Thread Windows"</span> )); 
    pFrameWnd->ShowWindow(SW_SHOW); 
    pFrameWnd->UpdateWindow(); 
    <span style="color: rgb(0, 0, 255);">return</span>  TRUE; 
} 
</div><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">6.在CMyWnd类中修改构造函数<span style="color: rgb(255, 102, 0);">CMyWnd()</span> 为<span style="color: rgb(255, 0, 0);">public</span> 属性,然后实现鼠标左键消息响应函数:</p><div style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px; border: 1px dashed rgb(204, 204, 204); background-color: rgb(239, 239, 239);"><span style="color: rgb(0, 0, 255);">void</span>  CMyWnd::OnLButtonDown(UINT nFlags, CPoint point) 
{ 
    LPTSTR pMessage = _T(<span style="color: rgb(163, 21, 21);">"This is a window thread"</span> ); 
    CWnd *pMainWnd = AfxGetMainWnd(); 
    ::MessageBox(NULL, pMessage, _T(<span style="color: rgb(163, 21, 21);">"Thread Message"</span> , MB_OK)); 
    CFrameWnd::OnLButtonDown(nFlags, point); 
} 
</div><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">7.在应用程序视图类中实现鼠标左键响应函数:</p><div style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px; border: 1px dashed rgb(204, 204, 204); background-color: rgb(239, 239, 239);"><span style="color: rgb(0, 0, 255);">void</span>  CThreadTestView::OnLButtonDown(UINT nFlags, CPoint point) 
{ 
    AfxBeginThread(RUNTIME_CLASS(CMyThread)); 
    CView::OnLButtonDown(nFlags, point); 
} 
</div><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">程序运行结果:</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;"><img src="http://hi.csdn.net/attachment/201007/24/0_1279982770574h.gif" alt="" style="border: none; max-width: 602px; height: auto;" /></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">这里要注意的是,这个用户界面不是属于主框架窗口的,可以说是和主线程窗口并列的。通过查看桌面任务栏就可以发现,两个窗口并行着显示,而且互相不影响。注意上面给出的代码<span style="color: rgb(255, 102, 0);">MessageBox</span> 是引用API的,并且是无窗口,所以这里有三个并行的窗口,而且各不干扰。单独关闭用户界面线程的窗体,可以正常退出用户界面线程。但是如果直接关闭了主线程窗体,那么用户界面线程就会非法关闭,造成内存泄露。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;"><img src="http://hi.csdn.net/attachment/201007/24/0_1279982781Q4QP.gif" alt="" style="border: none; max-width: 602px; height: auto;" /></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">对于用户界面线程的正常退出,只要在用户界面线程内调用<span style="color: rgb(255, 102, 0);">PostQuitMessage</span> 即可,参数为0的话,代表成功完成。</p>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: