CWinThread Class
Represents a thread of execution within an application.
class CWinThread : public CCmdTarget
Members
Public Constructors
Name |
Description |
---|---|
Constructs a CWinThread object. |
Public Methods
Name |
Description |
---|---|
Starts execution of a CWinThread object. |
|
Override to clean up when your thread terminates. |
|
Retrieves a pointer to the main window for the thread. |
|
Gets the priority of the current thread. |
|
Override to perform thread instance initialization. |
|
Checks for special messages. |
|
Override to perform thread-specific idle-time processing. |
|
Posts a message to another CWinThread object. |
|
Filters messages before they are dispatched to the Windows functions TranslateMessage and DispatchMessage. |
|
Intercepts certain messages before they reach the application. |
|
Intercepts all unhandled exceptions thrown by the thread's message and command handlers. |
|
Contains the thread's message loop. |
|
Decrements a thread's suspend count. |
|
Controlling function for threads with a message pump. Override to customize the default message loop. |
|
Sets the priority of the current thread. |
|
Increments a thread's suspend count. |
Public Operators
Name |
Description |
---|---|
Retrieves the handle of the CWinThread object. |
Public Data Members
Name |
Description |
---|---|
Specifies whether to destroy the object at thread termination. |
|
Handle to the current thread. |
|
ID of the current thread. |
|
Pointer to the main window of the container application when an OLE server is in-place active. |
|
Holds a pointer to the application's main window. |
Remarks
The main thread of execution is usually provided by an object derived from CWinApp; CWinApp is derived from CWinThread. Additional CWinThread objects allow multiple threads within a given application.
There are two general types of threads that CWinThread supports: worker threads and user-interface threads. Worker threads have no message pump: for example, a thread that performs background calculations in a spreadsheet application. User-interface threads have a message pump and process messages received from the system. CWinApp and classes derived from it are examples of user-interface threads. Other user-interface threads can also be derived directly from CWinThread.
Objects of class CWinThread typically exist for the duration of the thread. If you wish to modify this behavior, set m_bAutoDelete to FALSE.
The CWinThread class is necessary to make your code and MFC fully thread-safe. Thread-local data used by the framework to maintain thread-specific information is managed by CWinThread objects. Because of this dependence on CWinThread to handle thread-local data, any thread that uses MFC must be created by MFC. For example, a thread created by the run-time function _beginthread, _beginthreadex cannot use any MFC APIs.
To create a thread, call AfxBeginThread. There are two forms, depending on whether you want a worker or user-interface thread. If you want a user-interface thread, pass to AfxBeginThread a pointer to the CRuntimeClass of your CWinThread-derived class. If you want to create a worker thread, pass to AfxBeginThread a pointer to the controlling function and the parameter to the controlling function. For both worker threads and user-interface threads, you can specify optional parameters that modify priority, stack size, creation flags, and security attributes. AfxBeginThread will return a pointer to your new CWinThread object.
Instead of calling AfxBeginThread, you can construct a CWinThread-derived object and then call CreateThread. This two-stage construction method is useful if you want to reuse the CWinThread object between successive creation and terminations of thread executions.
For more information on CWinThread, see the articles Multithreading with C++ and MFC, Multithreading: Creating User-Interface Threads, Multithreading: Creating Worker Threads, and Multithreading: How to Use the Synchronization Classes.
Inheritance Hierarchy
CWinThread
Requirements
Header: afxwin.h