Creating and Terminating a Thread

To create a thread, call the CreateThread function. The following code example shows the CreateThread function prototype.

HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes,
DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId );

Because Windows CE does not support the lpThreadAttributes and dwStackSize parameters, you must set them to NULL or 0. The following table describes the remaining CreateThread parameters.

Parameter
Description
lpStartAddress Points to the start of the thread routine
lpParameter Specifies an application-defined value that is passed to the thread routine
dwCreationFlags Set to 0 or CREATE_SUSPENDED
lpThreadId Points to a DWORD that receives the new thread's identifier

If CreateThread is successful, it returns the handle to the new thread and the thread identifier. You can also retrieve the thread identifier by calling the GetCurrentThreadId function from within the thread. In Windows CE, the value returned in GetCurrentThreadId is the actual thread handle. You can also retrieve a handle to the thread by calling the GetCurrentThread function. This function returns a pseudo-handle to the thread that is valid only while in the thread. If you specify CREATE_SUSPENDED in the dwCreationFlags parameter, the thread is created in a suspended state and must be resumed with a call to the ResumeThread function.

You can terminate a thread by calling ExitThread, which frees the resources that are used by a thread when they are no longer needed. Calling ExitThread for an application's primary thread causes the application to terminate.