SuspendThread

Summary

Suspends the execution of a thread.

Syntax

#include <windows.h>

DWORD WINAPI SuspendThread(
    HANDLE hThread
);

Parameters

hThreadHandle to the thread to be suspended. The handle must have THREAD_SUSPEND_RESUME access.

Return value

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.

SuccessPrevious suspend count (>=0)
Failure(DWORD)-1

Remarks

Example

#include <windows.h>
#include <stdio.h>

DWORD WINAPI ThreadProc(LPVOID lpParameter) {
    for (int i = 0; i < 10; ++i) {
        printf("Thread running: %d\n", i);
        Sleep(500);
    }
    return 0;
}

int main() {
    HANDLE hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
    if (hThread == NULL) {
        printf("CreateThread failed (%lu)\\n", GetLastError());
        return 1;
    }

    Sleep(1500); // Let the thread run a bit
    SuspendThread(hThread);
    printf("Thread suspended.\\n");
    Sleep(2000); // Simulate work while thread is paused
    ResumeThread(hThread);
    printf("Thread resumed.\\n");

    WaitForSingleObject(hThread, INFINITE);
    CloseHandle(hThread);
    return 0;
}