Suspends the execution of a thread.
#include <windows.h>
DWORD WINAPI SuspendThread(
HANDLE hThread
);
hThread | Handle 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
.
Success | Previous suspend count (>=0) |
---|---|
Failure | (DWORD)-1 |
SuspendThread
increase the suspend count; each call to ResumeThread
decrements it.#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;
}