TerminateThread

TerminateThread terminates a thread.

Syntax

BOOL TerminateThread(
    HANDLE hThread,
    DWORD ExitCode
);

Parameters

hThread

The thread to terminate.

ExitCode

The exit code for the thread. Use GetExitCodeThread to retrieve a thread's exit value.

Return Value

TRUE if the function succeeds, FALSE if the function fails

To get extended error information, call GetLastError.

Remarks

TerminateThread is used to cause a thread to exit. When this occurs, the target thread has no chance to execute any user-mode code.

NOTE: TerminateThread is a dangerous function that should only be used in the most extreme cases.

Call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination.

A thread cannot protect itself against TerminateThread, other than by controlling access to its handles.

If the target thread is the last thread of a process when this function is called, the thread's process is also terminated.

Terminating a thread does not necessarily remove the thread object from the system. A thread object is deleted when the last thread handle is closed.

Under Windows, when a thread's existence ends by being the target of a TerminateThread call, the thread's stack is not automatically freed, as it may contain useful debugging information. RTSS follows this behavior, so when an RTSS user thread is the target of a TerminateThread call, the memory for that thread's stack will not be returned to the non-paged memory pool. To preserve memory, we have provided a new control panel setting to modify this behavior. If you have RTX Runtime installed, and you enable Free Stack on Terminate Thread Calls in the , RTSS will free the memory used for the stack when a thread is terminated via TerminateThread.

Requirements

Minimum Supported Version RTX64 2013
Header windows.h
Library Rtx_Rtss.lib

See Also:

CreateThread

ExitThread

GetExitCodeThread