2 * Test case for PostThreadMessage
5 * See: LGPL.txt in top directory.
8 * Windows thread message queue test case.
9 * Derived from ../event/event.c in part.
16 HANDLE hWaitForFailure
;
17 HANDLE hOkToPostThreadMessage
;
18 HANDLE hOkToTerminate
;
20 DWORD WINAPI
thread( LPVOID crap
)
24 /* Failure case ... Wait for the parent to try to post a message
25 before queue creation */
26 printf( "Waiting to create the message queue.\n" );
28 WaitForSingleObject(hWaitForFailure
,INFINITE
);
30 printf( "Creating message queue.\n" );
32 /* "Create" a message queue */
33 PeekMessage( &msg
, NULL
, 0, 0, PM_NOREMOVE
);
35 printf( "Signalling the parent that we're ready.\n" );
37 /* Signal that it's ok to post */
38 SetEvent( hOkToPostThreadMessage
);
40 printf( "Listening messages.\n" );
42 /* Now read some messages */
43 while( GetMessage( &msg
, 0,0,0 ) ) {
44 printf( "Received message: %04x %04x %08lx\n",
45 (msg
.message
& 0xffff),
46 (msg
.wParam
& 0xffff),
51 printf( "Finished receiving messages.\n" );
52 SetEvent( hOkToTerminate
);
57 int main( int argc
, char **argv
)
61 printf( "Creating events\n" );
63 hOkToPostThreadMessage
= CreateEvent( NULL
, FALSE
, FALSE
, NULL
);
64 hOkToTerminate
= CreateEvent( NULL
, FALSE
, FALSE
, NULL
);
65 hWaitForFailure
= CreateEvent( NULL
, FALSE
, FALSE
, NULL
);
67 printf( "Created events\n" );
69 if( CreateThread( 0, 0, thread
, 0, 0, &id
) == NULL
) {
70 printf( "Couldn't create one thread.\n" );
74 printf( "Posting to non-existent queue\n" );
76 /* Check failure case */
77 assert( PostThreadMessage( id
, WM_USER
+ 0, 1, 2 ) == FALSE
);
79 printf( "Signalling thread to advance.\n" );
81 SetEvent( hWaitForFailure
);
83 printf( "Waiting for signal from thread.\n" );
84 WaitForSingleObject( hOkToPostThreadMessage
, INFINITE
);
86 printf( "Sending three messages, then quit.\n" );
87 assert( PostThreadMessage( id
, WM_USER
+ 0, 1, 2 ) );
88 assert( PostThreadMessage( id
, WM_USER
+ 1, 3, 4 ) );
89 Sleep( 500 ); /* Sleep a bit, so that the queue is empty for a bit. */
90 assert( PostThreadMessage( id
, WM_USER
+ 2, 5, 6 ) );
91 assert( PostThreadMessage( id
, WM_QUIT
, 0,0 ) );
93 WaitForSingleObject( hOkToTerminate
, INFINITE
);
94 printf( "Test complete.\n" );