MFC界面线程

    CWinThread* m_pLoginThread;


    if(m_pLoginThread)

{

CloseLoginThread();

}


m_pLoginThread = AfxBeginThread(RUNTIME_CLASS(CLoginThreadUI));

m_pLoginThread->PostThreadMessage(WM_SET_PARENT,(WPARAM)this,NULL);


        WaitForSingleObject(m_pLoginThread->m_hThread, INFINITE);




void CXXApp::CloseLoginThread()

{

if(m_pLoginThread) 

{

// 1. 发一个WM_QUIT 消息结 UI 线程

m_pLoginThread->PostThreadMessage(WM_QUIT, NULL, NULL);


// 2. 等待 UI 线程正常退出

if (WAIT_OBJECT_0 == WaitForSingleObject(m_pLoginThread->m_hThread, INFINITE))

{

// 3. 删除 UI 线程对象,只有当你设置了m_bAutoDelete = FALSE; 时才调用

// delete   pUIThread1; 

}

}

}

///////////////////////////////////////////////////

#pragma once


#include "Call_loginDlg.h"


// CLoginThreadUI

#define WM_SET_PARENT WM_USER+1003


class CLoginThreadUI : public CWinThread

{

DECLARE_DYNCREATE(CLoginThreadUI)


protected:

CLoginThreadUI();           // 动态创建所使用的受保护的构造函数

virtual ~CLoginThreadUI();


public:

virtual BOOL InitInstance();

virtual int ExitInstance();


protected:

DECLARE_MESSAGE_MAP()


public:

CCall_loginDlg* m_pDlg;


void OnSetParent(WPARAM wParam,LPARAM lParam);

};


///////////////////////////////////////////////

// LoginThreadUI.cpp : 实现文件

//


#include "stdafx.h"

#include "Call_login.h"

#include "LoginThreadUI.h"



// CLoginThreadUI


IMPLEMENT_DYNCREATE(CLoginThreadUI, CWinThread)


CLoginThreadUI::CLoginThreadUI()

{

}


CLoginThreadUI::~CLoginThreadUI()

{

if(m_pDlg)

{

delete m_pDlg;

}

}


BOOL CLoginThreadUI::InitInstance()

{

// TODO: 在此执行任意逐线程初始化

return TRUE;

}


int CLoginThreadUI::ExitInstance()

{

// TODO: 在此执行任意逐线程清理

return CWinThread::ExitInstance();

}


BEGIN_MESSAGE_MAP(CLoginThreadUI, CWinThread)


ON_THREAD_MESSAGE(WM_SET_PARENT,OnSetParent)

END_MESSAGE_MAP()


void CLoginThreadUI::OnSetParent( WPARAM wParam,LPARAM lParam )

{

CCall_loginApp* pApp = (CCall_loginApp*)wParam;


m_pDlg = new CCall_loginDlg;

m_pDlg->Create(IDD_CALL_LOGIN_DIALOG);

m_pDlg->SetParentApp(pApp);

m_pDlg->CenterWindow();

m_pDlg->ShowWindow(TRUE);

}



// CLoginThreadUI 消息处理程序

/////////////////////////////////////////////////////////////////////////////

void CXXDlg::OnBnClickedCancel()

{

// TODO: 在此添加控件通知处理程序代码


m_pApp->m_pLoginThread->PostThreadMessage(WM_QUIT, NULL, NULL);


OnCancel();

}


本文出自 “日知其所无” 博客,谢绝转载!

你可能感兴趣的:(线程,mfc,界面)