JavaScript EditorDhtml editor     Free javascript download 

Main Page

A user-interface thread is commonly used to handle user input and respond to user events independently of threads executing other portions of the application. The main application thread (provided in your CWinApp-derived class) is already created and started for you. This topic describes the steps necessary to create additional user-interface threads.

The first thing you must do when creating a user-interface thread is derive a class from CWinThread. You must declare and implement this class, using the DECLARE_DYNCREATE and IMPLEMENT_DYNCREATE macros. This class must override some functions and can override others. These functions and what they should do are presented in the following table.

Functions to Override When Creating a User-Interface Thread
Function Purpose


Perform cleanup when thread terminates. Usually overridden.


Perform thread instance initialization. Must be overridden.


Perform thread-specific idle-time processing. Not usually overridden.


Filter messages before they are dispatched to TranslateMessage and DispatchMessage. Not usually overridden.


Intercept unhandled exceptions thrown by the thread's message and command handlers. Not usually overridden.


Controlling function for the thread. Contains the message pump. Rarely overridden.

MFC provides two versions of AfxBeginThread through parameter overloading: one for user-interface threads and the other for worker threads. To start your user-interface thread, call AfxBeginThread, providing the following information:

AfxBeginThread does most of the work for you. It creates a new object of your class, initializes it with the information you supply, and calls CWinThread::CreateThread to start executing the thread. Checks are made throughout the procedure to make sure all objects are deallocated properly should any part of the creation fail.

What do you want to know more about?

See Also

JavaScript EditorDhtml editor     Free javascript download