MFC多线程编程示例1

发布时间:2024年01月15日

新建一个对话框工程;

添加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->
文章来源:https://blog.csdn.net/bcbobo21cn/article/details/135591661
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。