1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include <windows.h>
#include <stdio.h>
DWORD WINAPI ThreadFct ( LPVOID lpParam ) {
HANDLE hEvent = (HANDLE)lpParam ;
printf("Starting secondary thread...\n");
MessageBox(NULL, "Thread function", "", MB_OK);
printf("Secondary thread : after MessageBox.\n");
if (SetEvent(hEvent))
return ERROR_SUCCESS ;
return ERROR_INVALID_HANDLE ;
}
void PrintWFSOResult ( LPCSTR Prefix, DWORD Result ) {
printf("%s : ",Prefix);
switch (Result) {
case WAIT_FAILED : printf("Failed.\n"); break ;
case WAIT_ABANDONED : printf("Abandoned.\n"); break ;
case WAIT_TIMEOUT : printf("Timeout.\n"); break ;
default : printf("WAIT_OBJECT_%lu.\n",Result);
}
}
void main ( void ) {
HANDLE hThread = NULL;
DWORD dwThdId;
HANDLE hEvent ;
DWORD WFSOResult ;
printf("Starting main thread...\n");
hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
hThread = CreateThread(NULL,0,ThreadFct,(LPVOID)hEvent,0,&dwThdId);
printf("Main thread : after thread creation.\n");
WFSOResult=WaitForSingleObject(hEvent,INFINITE);
PrintWFSOResult("Main thread : WaitFor Event",WFSOResult);
WFSOResult=WaitForSingleObject(hThread,INFINITE);
PrintWFSOResult("Main thread : WaitFor Thread",WFSOResult);
CloseHandle(hEvent);
CloseHandle(hThread);
} |
Partager