CWinThread Class

Represents a thread of execution within an application.

class CWinThread : public CCmdTarget

Members

Public Constructors

Name

Description

CWinThread::CWinThread

Constructs a CWinThread object.

Public Methods

Name

Description

CWinThread::CreateThread

Starts execution of a CWinThread object.

CWinThread::ExitInstance

Override to clean up when your thread terminates.

CWinThread::GetMainWnd

Retrieves a pointer to the main window for the thread.

CWinThread::GetThreadPriority

Gets the priority of the current thread.

CWinThread::InitInstance

Override to perform thread instance initialization.

CWinThread::IsIdleMessage

Checks for special messages.

CWinThread::OnIdle

Override to perform thread-specific idle-time processing.

CWinThread::PostThreadMessage

Posts a message to another CWinThread object.

CWinThread::PreTranslateMessage

Filters messages before they are dispatched to the Windows functions TranslateMessage and DispatchMessage.

CWinThread::ProcessMessageFilter

Intercepts certain messages before they reach the application.

CWinThread::ProcessWndProcException

Intercepts all unhandled exceptions thrown by the thread's message and command handlers.

CWinThread::PumpMessage

Contains the thread's message loop.

CWinThread::ResumeThread

Decrements a thread's suspend count.

CWinThread::Run

Controlling function for threads with a message pump. Override to customize the default message loop.

CWinThread::SetThreadPriority

Sets the priority of the current thread.

CWinThread::SuspendThread

Increments a thread's suspend count.

Public Operators

Name

Description

CWinThread::operator HANDLE

Retrieves the handle of the CWinThread object.

Public Data Members

Name

Description

CWinThread::m_bAutoDelete

Specifies whether to destroy the object at thread termination.

CWinThread::m_hThread

Handle to the current thread.

CWinThread::m_nThreadID

ID of the current thread.

CWinThread::m_pActiveWnd

Pointer to the main window of the container application when an OLE server is in-place active.

CWinThread::m_pMainWnd

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

CObject

CCmdTarget

CWinThread

Requirements

Header: afxwin.h

See Also

Reference

CCmdTarget Class

Hierarchy Chart

CWinApp Class

CCmdTarget Class