MFC 多线程.7z


程序使用TAB控件生成了3个Dialog,每个dialog上都有一个简单的MFC多线程实例,简单易懂,且有注释,是MFC多线程学习的好帮手,多线程传参数:传递窗口句柄等等
资源截图
代码片段和文件信息
// Dialog1.cpp : implementation file
//

#include “stdafx.h“
#include “MultiThread11.h“
#include “Dialog1.h“
extern m_flag;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDialog1 dialog

BOOL m_flag = TRUE;
CDialog1::CDialog1(CWnd* pParent /*=NULL*/)
: CDialog(CDialog1::IDD pParent)
{
//{{AFX_DATA_INIT(CDialog1)
//}}AFX_DATA_INIT
}


void CDialog1::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDialog1)
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialog1 CDialog)
//{{AFX_MSG_MAP(CDialog1)
ON_BN_CLICKED(IDC_BUTTON1 OnButton1)
ON_BN_CLICKED(IDC_BUTTON2 OnButton2)
ON_BN_CLICKED(IDC_BUTTON3 OnButton3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialog1 message handlers
void  ThreadFunc(LPVOID pParam)
{
CString strTime;
CDialog1 *dlg;
dlg = (CDialog1 *)pParam;
CTime time;
m_flag = TRUE;
while(m_flag)
{
time = CTime::GetCurrentTime();
strTime=time.Format(“%H:%M:%S“);
::SetDlgItemText(dlg->m_hWndIDC_EDIT1strTime);
// GetDlgItem(IDC_EDIT1)->setwindow
Sleep(1000);
}
ExitThread(1);

}

void CDialog1::OnButton1() //启动线程//传递的参数 是 窗口对象指针
{
// TODO: Add your control notification handler code here
hThread=CreateThread(NULL
0
(LPTHREAD_START_ROUTINE)ThreadFunc
this
0
&ThreadID);
GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON2)->EnableWindow(TRUE);

}

void CDialog1::OnButton2() 
{
// TODO: Add your control notification handler code here
m_flag = false;
GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
}

void CDialog1::OnButton3() 
{
// TODO: Add your control notification handler code here
TerminateThread(hThread1);
GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)