Книга: Microsoft Windows Embedded CE 6.0 Exam Preparation Kit

Thread Management Sample Code

Thread Management Sample Code

The following code snippet illustrates how to create a thread in suspended mode, specify a thread function and parameters, change the thread priority, resume the thread, and wait for the thread to finish its processing and exit. In the last step, the following code snippet demonstrates how to check the error code returned from the thread function.

// Structure used to pass parameters to the thread.
typedef struct {
 BOOL bStop;
} THREAD_PARAM_T
// Thread function
DWORD WINAPI ThreadProc(LPVOID lpParameter) {
 // Perform thread actions...
 // Exit the thread.
 return ERROR_SUCCESS;
}
BOOL bRet = FALSE;
THREAD_PARAM_T threadParams;
threadParams.bStop = FALSE;
DWORD dwExitCodeValue = 0;
// Create the thread in suspended mode.
HANDLE hThread = CreateThread(NULL, 0, ThreadProc,
 (LPVOID) &threadParams, CREATE_SUSPENDED, NULL);
if (hThread == NULL) {
 // Manage the error...
} else {
 // Change the Thread priority.
 CeSetThreadPriority(hThread, 200);
 // Resume the thread, the new thread will run now.
 ResumeThread(hThread);
 // Perform parallel actions with the current thread...
 // Wait until the new thread exits.
 WaitForSingleObject(hThread, INFINITE);
 // Get the thread exit code
 // to identify the reason for the thread exiting
 // and potentially detect errors
 // if the return value is an error code value.
 bRet = GetExitCodeThread(hThread, &dwExitCodeValue);
 if (bRet && (ERROR_SUCCESS == dwExitCodeValue)) {
  // Thread exited without errors.
 } else {
  // Thread exited with an error.
 }
 // Don't forget to close the thread handle
 CloseHandle(hThread);
}

Оглавление книги


Генерация: 1.536. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз