Skip to content

Latest commit

 

History

History
60 lines (43 loc) · 4.81 KB

multithreading-creating-user-interface-threads.md

File metadata and controls

60 lines (43 loc) · 4.81 KB
title ms.custom ms.date ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
Multithreading: Creating User-Interface Threads | Microsoft Docs
11/04/2016
cpp-windows
article
CREATE_SUSPENDED
SECURITY_ATTRIBUTES
C++
multithreading [C++], user interface threads
threading [C++], creating threads
threading [C++], user interface threads
user interface threads [C++]
threading [MFC], user interface threads
446925c1-db59-46ea-ae5b-d5ae5d5b91d8
9
mikeblome
mblome
ghogen

Multithreading: Creating User-Interface Threads

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

|ExitInstance|Perform cleanup when thread terminates. Usually overridden.|
|InitInstance|Perform thread instance initialization. Must be overridden.|
|OnIdle|Perform thread-specific idle-time processing. Not usually overridden.|
|PreTranslateMessage|Filter messages before they are dispatched to TranslateMessage and DispatchMessage. Not usually overridden.|
|ProcessWndProcException|Intercept unhandled exceptions thrown by the thread's message and command handlers. Not usually overridden.|
|Run|Controlling function for the thread. Contains the message pump. Rarely overridden.|

MFC provides two versions of AfxBeginThread through parameter overloading: one that can only create worker threads and one that can create user-interface threads or worker threads. To start your user-interface thread, call the second overload of AfxBeginThread, providing the following information:

  • The RUNTIME_CLASS of the class you derived from CWinThread.

  • (Optional) The desired priority level. The default is normal priority. For more information about the available priority levels, see SetThreadPriority in the [!INCLUDEwinsdkshort].

  • (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 about the format of this security information, see SECURITY_ATTRIBUTES in the [!INCLUDEwinsdkshort].

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

Multithreading with C++ and MFC