Tuesday, December 19, 2006

CreateEvent Sample code

#include "afxwin.h"
#include
#include

DWORD ThreadFunction( LPVOID lpArg );

void main()
{
HANDLE hEvent;

HANDLE hThread;

DWORD dwThreadID;

hEvent = CreateEvent( NULL, FALSE, FALSE, NULL );

if( hEvent == NULL ) //oops!
{

printf( "CreateEvent() failed!\n" );
return;
}

hThread = CreateThread( NULL, NULL, (LPTHREAD_START_ROUTINE)ThreadFunction,

(LPVOID)hEvent, NULL, &dwThreadID );

if( hThread == NULL ) //oops!
{
printf( "CreateThread() failed!\n" );

CloseHandle( hEvent );

return;
}
printf( "\nmain() - Waiting..." );

WaitForSingleObject( hEvent, INFINITE );

printf( "\nmain() - wait over.\n" );

//clean-up

CloseHandle( hEvent );

CloseHandle( hThread );

printf( "\nPress any key.\n" );

getch();
}
DWORD ThreadFunction( LPVOID lpArg )
{

HANDLE hEvent = (HANDLE)lpArg;

printf( "\nThreadFunction() - Sleeping for 4 seconds..." );

Sleep( 4000 ); //sleep for 4 seconds

printf( "done. Setting event.\n" );

SetEvent( hEvent ); //set the event state to signalled

return 0L;
}