Terminating a Thread and Thread Exit Codes

A thread terminates when the thread function exits. The return value from the thread function is the thread's exit code. The exit code can be used to communicate success or failure to other threads. A thread function, or any function it calls, can terminate prematurely by calling ExitThread. This function is passed a single DWORD parameter that is used as the thread's exit code:

ExitThread(10); // set thread exit code to 10

The code in Listing 5.8 creates a thread, and then calls WaitForSingleObject to block the primary thread until this thread terminates, or until 5000 milliseconds have elapsed. It is a good idea to set a timeout on waiting for a thread just in case the thread function fails. The ...

Get Windows® CE 3.0 Application Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.