直接上代码。代码如下:
1.CMyForm.h
#pragma once
#include "resource.h"
#include "CMyThread.h"
// CMyForm 窗体视图
class CMyForm : public CFormView
{
DECLARE_DYNCREATE(CMyForm)
protected:
CMyForm(); // 动态创建所使用的受保护的构造函数
virtual ~CMyForm();
CMyThread *p_thread;
public:
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_FORMVIEW };
#endif
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
public:
CButton m_sendButton1;
CEdit m_text1;
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton2();
afx_msg void OnBnClickedButton3();
afx_msg LRESULT OnWmEnd(WPARAM wParam, LPARAM lParam);
public:
CEdit m_print;
};
2、CMyForm.cpp
// CMyForm.cpp: 实现文件
//
#include "pch.h"
#include "MFCApplication31.h"
#include "CMyForm.h"
#include <memory>
#include <string>
using namespace std;
// CMyForm
IMPLEMENT_DYNCREATE(CMyForm, CFormView)
CMyForm::CMyForm()
: CFormView(IDD_FORMVIEW),p_thread(nullptr)
{
}
CMyForm::~CMyForm()
{
if (p_thread != nullptr)
{
p_thread->PostThreadMessage(WM_QUIT, 18L, 0L);
::WaitForSingleObject(p_thread->m_hThread, INFINITE);
p_thread->m_bAutoDelete = TRUE;
p_thread->Delete();
p_thread = nullptr;
}
}
void CMyForm::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
DDX_Control(pDX, IDC_BUTTON1, m_sendButton1);
DDX_Control(pDX, IDC_EDIT1, m_text1);
DDX_Control(pDX, IDC_EDIT2, m_print);
}
BEGIN_MESSAGE_MAP(CMyForm, CFormView)
ON_BN_CLICKED(IDC_BUTTON1, &CMyForm::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CMyForm::OnBnClickedButton2)
// ON_MESSAGE(WM_ABC, &CMyForm::OnAbc)
ON_BN_CLICKED(IDC_BUTTON3, &CMyForm::OnBnClickedButton3)
ON_MESSAGE(WM_END, &CMyForm::OnWmEnd)
END_MESSAGE_MAP()
// CMyForm 诊断
#ifdef _DEBUG
void CMyForm::AssertValid() const
{
CFormView::AssertValid();
}
#ifndef _WIN32_WCE
void CMyForm::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif
#endif //_DEBUG
// CMyForm 消息处理程序
BOOL CMyForm::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style &=~WS_BORDER;
return CFormView::PreCreateWindow(cs);
}
void CMyForm::OnBnClickedButton1()
{
//利用智能指针new一个CString
std::unique_ptr<CString> m_message = std::make_unique<CString>();
//取得消息的文本放入m_message中。
m_text1.GetWindowText(*(m_message.get()));
//发送一个消息。
if(p_thread!=nullptr)
p_thread->PostThreadMessage(WM_MESSAGE1, reinterpret_cast<WPARAM>(m_message.release()), 0L);
}
void CMyForm::OnBnClickedButton2()
{
// 向线程发送结束消息
if (p_thread != nullptr)
{
//p_thread->PostThreadMessage(WM_QUIT, 18L, 0L);
p_thread->PostThreadMessage(WM_MESSAGE2, 0L, 0L);
}
}
void CMyForm::OnBnClickedButton3()
{
// 启动线程
m_print.SetWindowText(TEXT(""));
if (p_thread == NULL)
{
p_thread = DYNAMIC_DOWNCAST(CMyThread, AfxBeginThread(RUNTIME_CLASS(CMyThread), 0, 0U, CREATE_SUSPENDED));
if (p_thread != NULL)
{
//让线程不自动删除
p_thread->m_bAutoDelete = FALSE;
//唤醒线程
p_thread->ResumeThread();
}
}
}
afx_msg LRESULT CMyForm::OnWmEnd(WPARAM wParam, LPARAM lParam)
{
if (p_thread != nullptr)
{
::WaitForSingleObject(p_thread->m_hThread, INFINITE);
p_thread->m_bAutoDelete = TRUE;
p_thread->Delete();
p_thread = nullptr;
m_print.SetWindowText(TEXT("线程已经注销"));
}
return 0;
}
3、CMyThread.h
#pragma once
// CMyThread
class CMyThread : public CWinThread
{
DECLARE_DYNCREATE(CMyThread)
protected:
CMyThread(); // 动态创建所使用的受保护的构造函数
virtual ~CMyThread();
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnMessage1(WPARAM wp, LPARAM lp);
//处理结束消息
afx_msg void OnMessage2(WPARAM wp, LPARAM lp);
};
4.CMyThread.cpp
// CMyThread.cpp: 实现文件
//
#include "pch.h"
#include "MFCApplication31.h"
#include "CMyThread.h"
#include <memory>
#include <string>
using namespace std;
// CMyThread
IMPLEMENT_DYNCREATE(CMyThread, CWinThread)
CMyThread::CMyThread()
{
}
CMyThread::~CMyThread()
{
}
BOOL CMyThread::InitInstance()
{
return TRUE;
}
int CMyThread::ExitInstance()
{
return CWinThread::ExitInstance();
}
BEGIN_MESSAGE_MAP(CMyThread, CWinThread)
ON_THREAD_MESSAGE(WM_MESSAGE1,&CMyThread::OnMessage1)
ON_THREAD_MESSAGE(WM_MESSAGE2, &CMyThread::OnMessage2)
END_MESSAGE_MAP()
// CMyThread 消息处理程序
void CMyThread::OnMessage1(WPARAM wp, LPARAM lp)
{
std::unique_ptr<CString> message(reinterpret_cast<CString *>(wp));
#ifdef _DEBUG
//Sleep(50000);
OutputDebugString(message->GetString());
#endif
}
void CMyThread::OnMessage2(WPARAM wp, LPARAM lp)
{
//处理主线程发送的结束消息
::PostQuitMessage(WM_QUIT);
//m_pMainWnd上主窗口hWnd的临时变量,目的是方便工作线程向主窗口发送消息。
CWnd *temwnd = m_pMainWnd->GetDlgItem(AFX_IDW_PANE_FIRST);
if (temwnd != nullptr)
temwnd->PostMessage(WM_END);
#ifdef _DEBUG
OutputDebugString(TEXT("QUIT\n"));
#endif
}
5.在Resouces.h中添加
#define WM_MESSAGE1 WM_APP+1
#define WM_MESSAGE2 WM_APP+2
#define WM_END WM_APP+3