MFC多线程编程示例1

新建一个对话框工程;

添加2个编辑框,2个按钮;

对话框头文件添加,

public:
	CWinThread *m_pthread1;
	CWinThread *m_pthread2;
	static UINT hellothread(LPVOID lparam);
	static UINT testthread(LPVOID lparam);
	CCriticalSection g_criticalsection;
	BOOL flag;
	int i1, i2;

对话框构造函数中添加,flag = true;

OnInitDialog()中添加,

	i1 = 0;
	i2 = 0;
	m_pthread1 = AfxBeginThread(hellothread, this, THREAD_PRIORITY_NORMAL, 0, 0);
	m_pthread2 = AfxBeginThread(testthread, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);

线程函数和按钮单击代码;

UINT CthrddemoDlg::hellothread(LPVOID lparam)
{
	CthrddemoDlg *threadol = (CthrddemoDlg*)lparam;

	while (threadol->flag)
	{
		//threadol->g_criticalsection.Lock();
		threadol->i1 = threadol->i1 + 1;
		threadol->SetDlgItemInt(IDC_EDIT1, threadol->

你可能感兴趣的:(VC++,mfc,c++,多线程)