Multithreading: Creating User-Interface Threads
| Overview | How Do I | Sample
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 article 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 . You must declare and implement this class, using the and 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
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 , providing the following information:
(Optional) The desired priority level. The default is normal priority. For more information on the available priority levels, see in the Win32 Programmer’s Reference.
(Optional) The desired stack size for the thread. The default is the same size stack as the creating thread.
(Optional) CREATE_SUSPENDED if you want the thread to be created in a suspended state. The default is 0, or start the thread normally.
(Optional) The desired security attributes. The default is the same access as the parent thread. For more information on the format of this security information, see in the Win32 Programmer’s Reference.
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 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.