PostHeaderIcon Real UI Thread With Win32API

Intro:

In last article,we demo a worker thread with UI,you know it is not a UI thread at all.

Now let’s create the real ui thread,sure with the Win32API – AfxBeginThread.

We just based on yesterday’s code and UI.

A little diff is we use the  form creates a user-interface thread,below is it’s declaration:

CWinThread* AfxBeginThread(
	CRuntimeClass* pThreadClass,
	int nPriority = THREAD_PRIORITY_NORMAL,
	UINT nStackSize = 0,
	DWORD dwCreateFlags = 0,
	LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL 
	);

This form creates a user-interface thread[MSDN].
All you need to know or want to know is all on MSDN.
In Action:
No1,define a thread class based on CWinThread,in this article it’s CUIThread.

// CUIThread

class CUIThread : public CWinThread
{
	DECLARE_DYNCREATE(CUIThread)

protected:
	CUIThread();           // dynamically create a protected constructor
	virtual ~CUIThread();

public:
	virtual BOOL InitInstance();
	virtual int ExitInstance();

protected:
	DECLARE_MESSAGE_MAP()
private:
	CUIDialog m_dialog;
};

No2,insert a dialog(IDD_UI_DIALOG) resource as the UI thread’s UI.

No3,create a class CUIDialog from IDD_UI_DIALOG dialog resource.

// context code
public:
	CUIDialog(CWnd* pParent = NULL);   
	virtual ~CUIDialog();

// dialog data
	enum { IDD = IDD_UI_DIALOG };
// context code

No4,add two ctrls on CUIDialog as picture shows above and add them as control variables into class CUIDialog.

// UIDialog.h
CProgressCtrl m_ProCtrl;
CStatic m_ProText;
// UIDialog.cpp
DDX_Control(pDX, IDC_UI_PRO, m_ProCtrl);
DDX_Control(pDX, IDC_UI_PRO_TEXT, m_ProText);

No5,add member m_Ticks and WM_TIMER message to CUIDialog.

// UIDialog.h
int m_Ticks;
afx_msg void OnTimer(UINT_PTR nIDEvent);
// UIDialog.cpp
void CUIDialog::OnTimer(UINT_PTR nIDEvent)
{
	m_ProCtrl.SetPos(m_Ticks);
	CString s;
	s.Format(_T("%d%%"),m_Ticks);
	m_ProText.SetWindowTextW(s);
	if(m_Ticks==100)
		m_Ticks=0;
	++m_Ticks;

	CDialogEx::OnTimer(nIDEvent);
}

No6,init m_Ticks and set timer in CUIDialog::OnInitDialog().

m_Ticks=0;
SetTimer(1,100,NULL);

No7,overwrite the InitInstance()[must be overwrited] method in CUIThread inherits from CWinThread.

// UIThread.h
public:
	virtual BOOL InitInstance();
	virtual int ExitInstance();
private:
	CUIDialog m_dialog;
// UIThread.cpp
BOOL CUIThread::InitInstance()
{
	//every UI thread target with a new UI Dialog as it's UI
	m_pMainWnd=&m_dialog;
	m_dialog.Create(IDD_UI_DIALOG);
	m_dialog.UpdateWindow();
	m_dialog.ShowWindow(SW_SHOW);

	return TRUE;
}

No8, a little work have to do while exit the UI thread,overwrite the ExitInstance()[usually overwrite] method in CWinThread.

int CUIThread::ExitInstance()
{
	PostQuitMessage(0);
	return CWinThread::ExitInstance();
}

why overwrite the ExitInstance() method?

Normal thread termination for the user interface thread, the process is very simple: from the user interface thread calls the Windows SDK PostQuitMessage. PostQuitMessage only parameter used is the thread exit code. For the worker threads, 0 usually indicates successful completion.[MSDN]

No9,call AfxBeginThread() method to response button click event.

void CSingleThreadDlg::OnBnClickedRealUi()
{
	AfxBeginThread(RUNTIME_CLASS(CUIThread));
}

No10,Test it.

main dlg

results

Summary:

With worker thread,UI thread is a little complex,

more information about ui-thread description and examples you can find from MSDN.

enjoy it.

18,934 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