Wait_Events.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 3k
Development Platform:

C++ Builder

  1. #include <genstub.cpp>
  2. // Child thread procedure. The child waits to get event. It then sits idle
  3. // for five seconds and sets the event again so another thread can use it.
  4. DWORD WINAPI ChildThreadProc( HWND hWnd )
  5. {
  6.    char szBuffer[256];         // work area for print formatting
  7.    HANDLE hAutoEvent = OpenEvent( SYNCHRONIZE, FALSE, "EXAMPLE-AUTOEVENT");
  8.    wsprintf( szBuffer, "Thread %x waiting for Event %x",
  9.             GetCurrentThreadId(  ), hAutoEvent );
  10.    SendMessage( hWnd, WM_USER, 0, (LPARAM)szBuffer );
  11.    // Check that write auto reset is signaled.
  12.    WaitForSingleObject( hAutoEvent, INFINITE );
  13.    wsprintf( szBuffer,"Thread %x got event", GetCurrentThreadId( ) );
  14.    SendMessage( hWnd, WM_USER, 0, (LPARAM)szBuffer );
  15.    Sleep( 5000 );
  16.    // Release event.
  17.    wsprintf( szBuffer,"Thread %x is done with event", GetCurrentThreadId( ) );
  18.    SendMessage( hWnd, WM_USER, 0, (LPARAM)szBuffer );
  19.    SetEvent( hAutoEvent );
  20.    CloseHandle( hAutoEvent );
  21.    ExitThread( TRUE );
  22. }
  23. // Windows message procedure.
  24. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  25. {
  26.    static HANDLE hAutoEvent = 0;
  27.    switch (uMsg)
  28.    {
  29.            case WM_CREATE: // Make an auto-reset event with initial state of signaled.
  30.                    hAutoEvent = CreateEvent( NULL, FALSE, TRUE, "EXAMPLE-AUTOEVENT");
  31.                    return DefWindowProc( hWnd, uMsg, wParam, lParam );
  32.            case WM_DESTROY:
  33.                    if ( hAutoEvent )
  34.                       CloseHandle( hAutoEvent );
  35.                    PostQuitMessage( 0 );
  36.                    break;
  37.            case WM_USER:
  38.                    {  // Message to show synchronization actions.
  39.                       TCHAR szBuffer[101];
  40.                       static int row = 0;
  41.                       static int msg_num = 1;
  42.                       HDC hDC = GetDC( hWnd );
  43.                       FillMemory( szBuffer, 100, 32 );
  44.                       TextOut( hDC, 0, row, szBuffer, 100 );
  45.                       wsprintf( szBuffer, "%3d: %s", msg_num++, (LPTSTR)lParam );
  46.                       TextOut( hDC, 0, row, szBuffer, lstrlen( szBuffer ) );
  47.                       row = ( row > 200 ) ? 0 : row+=20;
  48.                       ReleaseDC( hWnd, hDC );
  49.                    }
  50.                    break;
  51.            case WM_COMMAND:       // process menu items
  52.                    switch ( LOWORD( wParam )  )
  53.                    {
  54.                       case IDM_TEST:
  55.                       {
  56.                            DWORD id = 0;
  57.                            CreateThread( NULL, 0, ChildThreadProc, hWnd, 0, &id );
  58.                       }
  59.                       break;
  60.                       case IDM_EXIT:
  61.                            DestroyWindow( hWnd );
  62.                       break;
  63.                    }
  64.            break;
  65.            default:
  66.                 return DefWindowProc( hWnd, uMsg, wParam, lParam );
  67.    }
  68.    return NULL;
  69. }