PostHeaderIcon Win32 Multi-Threads Programming

I’m sorry,long time has no updates.This article is also about thread,of course,multi-thread.

First,let’s see the result of this demo:

pics of one:

UI thread

pics of two:

multi-thread

multi-thread

the source code of it,also for the previous article<Create UI thread when the application startup>:

Win32MultiThreads source code(zip)

From the pic two,you can see several tests:

  1. Timer thread test;
  2. count thread(with int param) test;
  3. count thread(with struct param) test;
  4. max threads test;
  5. startup thread you saw in last article.

startup thread we have discussed.now begin timer thread:

void ThreadFunc(LPVOID pParam)
{
	CWin32MultiThreadsDlg* pDlg=(CWin32MultiThreadsDlg*)pParam;
	CTime time;
	CStringW strTime;
	m_bRun=TRUE;
	while(m_bRun)
	{
		time=CTime::GetCurrentTime();
		strTime=time.Format("%H:%M:%S");
		CWnd* pc=AfxGetMainWnd();
		if(pc)
		{
			HWND hwnd=pc->m_hWnd;
			SetDlgItemText(hwnd,IDC_TIMER,strTime);
		}
		else
			pDlg->m_Time.SetWindowTextW(strTime);
		Sleep(1000);
		// ++pDlg->pro_num;
	}
}

First we get the pointer of the main dialog, then get the current time and show it with the dialog readonly edit control.

the time refreshed or update each second as a timer.the start and stop timer button with their code is simple.

Next pass a int param to a thread:

void CountThread( int integer )
{
	int i=0;
	while(i<integer)
	{
		Beep(200,50);
		Sleep(1000);
		++i;
	}
	HWND hMainWnd=AfxGetApp()->GetMainWnd()->m_hWnd;
	HWND hWnd=GetDlgItem(hMainWnd,IDC_START_COUNT);
	EnableWindow(hWnd,TRUE);
}

smart readers will find the different between the two thread.the param of each is different and the way get the control of dlg is diff.

two way we can get the control of the dlg,one with MFC method,one with Win32 API, this is the basic skill of MFC you have know,right?

Test thread pass struct param is similar with pass int param.

Max thread num test is demo for the max num threads your machine can create.

All above is not the hard part of the thread,the difficulty of multi-threads is they communicate with each other,I’ll demo it in next article.

117,109 views

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Copyright © 2010 - C++ Technology. All Rights Reserved.

Powered by Jerry | Free Space Provided by connove.com