Threads are light weight process. Main application creates these light weight process to do certain tasks in a separate independent context. This way main process can offload main task into several threads. This is possible as Windows is a multithreaded operating system. Several miniature threads can run in parallel. They can exit when they are done or they can be put to suspended until next work load comes.

Worker vs UI

There are two types of threads worker thread and user interface thread. A worker thread is a type of light weight process which is done in the background without any user interaction. UI thread is a process where a top level window and user interaction messages are involved. We can say UI thread is a front end task whereas worker threads are back end tasks.


AfxBeginThread() is a MFC global function which creates a new thread. AfxBeginThread is an overloaded function. It creates a worker thread if first argument is a thread function pointer else this can be a thread class for creating a UI thread. AfxBeginThread creates a new CWinThread object, calls its CreateThread function to start executing the thread, and returns a pointer to the thread. To end the thread, call AfxEndThread from within the thread, or return from the controlling function of the worker thread. MFC CWinThread class is the wrapper for thread related works. Parent can control this thread via its member functions.

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


  • pfnThreadProc - Points to the thread procedure function for the worker thread. Cannot be NULL. This function must be declared as follows: UINT ThreadProc( LPVOID pParam );
  • pThreadClass - The RUNTIME_CLASS of an object derived from CWinThread.
  • pParam - Parameter to be passed to the controlling function as shown in the parameter to the function declaration in pfnThreadProc.
  • nPriority - The desired priority of the thread. If 0, the same priority as the creating thread will be used. For a full list and description of the available priorities, see SetThreadPriority in the Win32 Programmer’s Reference.
  • nStackSize - Specifies the size in bytes of the stack for the new thread. If 0, the stack size defaults to the same size stack as the creating thread.
  • dwCreateFlags Specifies an additional flag that controls the creation of the thread. This flag can contain one of two values:
    • CREATE_SUSPENDED - Start the thread with a suspend count of one. The thread will not execute until ResumeThread is called.
    • 0 - Start the thread immediately after creation.
  • lpSecurityAttrs Points to a SECURITY_ATTRIBUTES structure that specifies the security attributes for the thread. If NULL, the same security attributes as the creating thread will be used. For more information on this structure, see the Win32 Programmer’s Reference.

Return Value

Pointer to the newly created thread object.

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...