PostHeaderIcon Create UI thread when the application startup

In this demo, I use win32 api to create thread.absolutely,I’ll use CWinThread in other demos.

The demo shows a  UI thread working when the application’s main thread startup.

Sometimes,the application may be large,If you have a friendly UI thread,it looks good. Especially if the application startup slowly.

One defect of this demo is I Just give the UI thread 7.5 seconds,the time depends on your application size.

The demo depends on the time but not other event messages,I will improve it to make it work better(change the progroess bar and the message event ).

OK,let’s first see the result of this:

UI thread

UI thread

The UI thread is working at the same time the application startup,of course,they are two different threads.

You can treat the main thread as the worker thread,In fact,it has its own interface thread and worker thread.

Have a startup UI thread,you have the follow steps to do:

1.Creat a Class based on CDialog(with CProgressCtrl and a static text),here I call this class name as CWin32ProcessDlg;
2.Declare the CWin32ProcessDlg variable at the appropriate place in your application;
3.Declare and define the UI thread function(in this thread create the startup UI thread dialog);
4.Create the UI thread in your application init part;
5.Test it.

Now the code of each step:

  • CWin32ProcessDlg:
/** other
  * context
  * code **/
public:
	CProgressCtrl m_ProcessCtrl;
	CStatic m_ProcessText;
	virtual BOOL OnInitDialog();
	afx_msg void OnDestroy();
/** other
  * context
  * code **/
BOOL CWin32ProcessDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// set the range of the progress ctrl
	m_ProcessCtrl.SetRange(0,10);

	return TRUE;
}
CWin32ProcessDlg::~CWin32ProcessDlg()
{
	DestroyWindow();
}
/** other
  * context
  * code **/
  • Declare the CWin32ProcessDlg variable:

the place of it is often in your application’s header file.

public:
	CWin32ProcessDlg m_ProDlg;
  • Declare and define the UI thread function:
void SubWinThread( LPVOID pParam )
{
	CWin32MultiThreadsDlg* pDlg=(CWin32MultiThreadsDlg*)pParam;

	pDlg->m_ProDlg.Create(IDD_PROCESS_DIALOG);
    // this is important to make sure the UI dialog is on top of your application window
	pDlg->m_ProDlg.SetParent(NULL);

	while(TRUE)
	{
		pDlg->m_ProDlg.m_ProcessCtrl.SetPos(pDlg->pro_num);
		if(pDlg->pro_num<=2)
            pDlg->m_ProDlg.m_ProcessText.
				SetWindowTextW(L"Initialize the application environment...");
		else if(pDlg->pro_num>2 && pDlg->pro_num<9)
            pDlg->m_ProDlg.m_ProcessText.
				SetWindowTextW(L"Application environment is initializing...");
		else
			pDlg->m_ProDlg.m_ProcessText.
				SetWindowTextW(L"End application environment initialization...");
		Sleep(750);

		if (pDlg->pro_num==10)
			break;

		++pDlg->pro_num;
	}
}
  • Create the UI thread in your application init part:
BOOL CWin32MultiThreadsDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	/** other
      * context
      * code **/

	// for UI
	pro_num=0;
	hSubThread=CreateThread(NULL,
		0,
		(LPTHREAD_START_ROUTINE)SubWinThread,
		(LPVOID)this,
		0,
		&SubThreadID);

	/** other
      * context
      * code **/

	return TRUE;
}
  • Test it:

UI thread begin:

thread_begin

thread_begin

UI thread working:

thread_working

thread_working

UI thread end:

thread_end

thread_end

I’ll make the code available for download in later demos,the code contains several demos.

114,937 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