WinAPI

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. Read the rest of this entry »

19,675 views

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. Read the rest of this entry »

15,083 views

PostHeaderIcon Win32 Worker Thread without UI

When you want to create a worker thread with Win32 API.

first,define a function like this: UNIT ThreadFunc(LPVOID pParam) ,the detail of it you can find on MSDN;

second,call the ThreadFunc with the Win32API – AfxBeginThread;

last,put the AfxBeginThread API in the right place.

You can find the AfxBeginThread API declarations on MSDN:

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

The first form of AfxBeginThread creates a worker thread. The second form creates a user-interface thread[From MSDN].

Read the rest of this entry »

15,441 views

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.

Read the rest of this entry »

117,109 views

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.

Read the rest of this entry »

115,100 views

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

Powered by Jerry | Free Space Provided by connove.com