Microsoft Docs

SuspendThread function

Suspends a thread.

Syntax

DWORD SuspendThread( HANDLE hThread );
Parameters
Return value
Remarks
Example
ParameterDescription
hThreadHandle to the thread to be suspended. The handle must have THREAD_SUSPEND_RESUME access.

If the function succeeds, the return value is the thread's previous suspend count.

If the function fails, the return value is (DWORD)-1. To get extended error information, call GetLastError.

  • A thread can be suspended multiple times. Each call increments the suspend count. The thread resumes execution only when the suspend count reaches zero.
  • Suspending a thread does not guarantee that it will not execute any code before the suspend takes effect. Use synchronization primitives for precise control.
  • Do not suspend a thread that holds a critical section or a mutex, as it can cause deadlocks.
  • Suspending a thread in a different process requires that the calling process have the SE_DEBUG_NAME privilege.

Below is a simple example that creates a thread, suspends it for a few seconds, and then resumes it.

#include <windows.h> #include <stdio.h> DWORD WINAPI ThreadFunc(LPVOID lpParam) { for (int i = 0; i < 5; ++i) { printf("Thread running: %d\n", i); Sleep(1000); } return 0; } int main(void) { HANDLE hThread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL); if (!hThread) { printf("CreateThread failed\n"); return 1; } Sleep(1500); // Let the thread run a bit printf("Suspending thread...\n"); SuspendThread(hThread); Sleep(3000); // Thread is suspended printf("Resuming thread...\n"); ResumeThread(hThread); WaitForSingleObject(hThread, INFINITE); CloseHandle(hThread); return 0; }