PostHeaderIcon Win32 Worker Thread With UI

Intro:

I just follow with last article,combine UI with number couter.

We use the same Win32API – AfxBeginThread

CWinThread* AfxBeginThread(
   AFX_THREADPROC pfnThreadProc,
   LPVOID pParam,
   int nPriority = THREAD_PRIORITY_NORMAL,
   UINT nStackSize = 0,
   DWORD dwCreateFlags = 0,
   LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
);

While the number counter thread working,a progrerss bar will show the process.
Also,based on dialog program same with yesterday,sure we will follow with yesterday program.

In Action:
No1,put new controls on the dialog window.

workerui

No2,add new variable with CProgressCtrl and CStatic.

DDX_Control(pDX, IDC_PROGRESS, m_ProgressCtrl);
DDX_Control(pDX, IDC_PROGREASS_TEXT, m_ProgressText);

No3,add new thread function,while counting refresh the progress bar.

UINT WorkerUIThread(LPVOID pPram)
{
	CSingleThreadDlg* pDlg=(CSingleThreadDlg*)pPram;
	CProgressCtrl* pProgressCtrl=&(pDlg->m_ProgressCtrl);
	for (int i=1;i<101;++i)
	{
		pProgressCtrl->SetPos(i);
		CString s;
		s.Format(_T("%d%%"),i);
		pDlg->m_ProgressText.SetWindowTextW(s);
		Sleep(100);
	}
	return 0;
}

No4,add button click event to call AfxBeginThread API.

void CSingleThreadDlg::OnBnClickedCreuiThread()
{
	m_ProgressCtrl.SetRange(0,100);
	AfxBeginThread(WorkerUIThread,(LPVOID)this);
}

No5,Test it.

output

Summary:

Above is just like a “user-interface thread”,actually it’s not.

We add the new ui worker thread just get help with the main window – CSingleThreadDlg.

when you click “newUIThread” button several times,you’ll find the confusion.

Yes,I’ll intro UI Thread next article.so, I hope you enjoy them~

15,084 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