Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. WaitForSingleObject Visual C++ bug

WaitForSingleObject Visual C++ bug

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestionannouncement
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    coco243
    wrote on last edited by
    #1

    Hi, I am following an example from a book, and I have an WaitForSingleObject that works just first time and I don't know how to resolve that. The main things are happening in the ChildThreadProc. At the first execution of the ChildThreadProc the procedure steps through all lines of code, but when is launched again, the execution stops at WaitForSingleObject, as if hAutoEvent isn't signaled or I don't know what is happening, and please help me to figure this out. So, In the WindProc main procedure when the window is created, is created the Event also:

    case WM_CREATE: // Make an auto-reset event with initial state of signaled
    hAutoEvent = CreateEvent ( NULL, FALSE, TRUE, "EXAMPLE-AUTOEVENT" );
    return DefWindowProc(hWnd, message, wParam, lParam);

    The example of WaitForSingleObject is presented in the following procedure:

    // Child thread procedure. The child waits to get event. It then sits idle
    // for five seconds and sets the event again so another thread can use it.

    DWORD WINAPI ChildThreadProc ( HWND hWnd )
    {
    char szBuffer[256]; //work area for print formatting
    HANDLE hAutoEvent = OpenEvent( SYNCHRONIZE, FALSE, "EXAMPLE-AUTOEVENT");
    wsprintf(szBuffer, "Thread %x waiting for Event %x", GetCurrentThreadId(), hAutoEvent );
    SendMessage(hWnd, WM_USER, 0, (LPARAM)szBuffer);

    // Check that write auto reset is signaled.
    WaitForSingleObject(hAutoEvent, INFINITE );
    wsprintf(szBuffer, " Thread %x got event", GetCurrentThreadId() );
    SendMessage(hWnd, WM\_USER, 0, (LPARAM)szBuffer );
    Sleep(2000);
    
    //Release event
    wsprintf(szBuffer, "Thread %x is dome with event", GetCurrentThreadId() );
    SendMessage(hWnd, WM\_USER, 0, (LPARAM)szBuffer);
    SetEvent(hAutoEvent);
    CloseHandle( hAutoEvent);
    ExitThread( TRUE );
    

    The ChildThreadProc is started here:

    case IDM_TEST:
    {
    DWORD id=0;
    CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ChildThreadProc, hWnd, 0, &id );
    }
    break;

    and here is where text out is handled:

    case WM_USER:
    {
    TCHAR szBuffer[101];
    static int row = 0;
    static int msg_num = 1;

    		HDC hDC = GetDC(hWnd);
    
    		FillMemory(szBuffer, 100, 32 );
    		TextOut( hDC, 0, row, szBuffer, 100 );
    		wsprintf(szBuffer, "%3d: %s", msg\_num++, (LPSTR)lParam );
    		TextOut(hDC, 0, row, szBuffer, lstrlen(szBuffer) );
            row = (row > 200 ) ? 0 : row += 20;
    		ReleaseDC(hWnd,hDC);
    
    	}
    	break;
    

    Please h

    L 1 Reply Last reply
    0
    • C coco243

      Hi, I am following an example from a book, and I have an WaitForSingleObject that works just first time and I don't know how to resolve that. The main things are happening in the ChildThreadProc. At the first execution of the ChildThreadProc the procedure steps through all lines of code, but when is launched again, the execution stops at WaitForSingleObject, as if hAutoEvent isn't signaled or I don't know what is happening, and please help me to figure this out. So, In the WindProc main procedure when the window is created, is created the Event also:

      case WM_CREATE: // Make an auto-reset event with initial state of signaled
      hAutoEvent = CreateEvent ( NULL, FALSE, TRUE, "EXAMPLE-AUTOEVENT" );
      return DefWindowProc(hWnd, message, wParam, lParam);

      The example of WaitForSingleObject is presented in the following procedure:

      // Child thread procedure. The child waits to get event. It then sits idle
      // for five seconds and sets the event again so another thread can use it.

      DWORD WINAPI ChildThreadProc ( HWND hWnd )
      {
      char szBuffer[256]; //work area for print formatting
      HANDLE hAutoEvent = OpenEvent( SYNCHRONIZE, FALSE, "EXAMPLE-AUTOEVENT");
      wsprintf(szBuffer, "Thread %x waiting for Event %x", GetCurrentThreadId(), hAutoEvent );
      SendMessage(hWnd, WM_USER, 0, (LPARAM)szBuffer);

      // Check that write auto reset is signaled.
      WaitForSingleObject(hAutoEvent, INFINITE );
      wsprintf(szBuffer, " Thread %x got event", GetCurrentThreadId() );
      SendMessage(hWnd, WM\_USER, 0, (LPARAM)szBuffer );
      Sleep(2000);
      
      //Release event
      wsprintf(szBuffer, "Thread %x is dome with event", GetCurrentThreadId() );
      SendMessage(hWnd, WM\_USER, 0, (LPARAM)szBuffer);
      SetEvent(hAutoEvent);
      CloseHandle( hAutoEvent);
      ExitThread( TRUE );
      

      The ChildThreadProc is started here:

      case IDM_TEST:
      {
      DWORD id=0;
      CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ChildThreadProc, hWnd, 0, &id );
      }
      break;

      and here is where text out is handled:

      case WM_USER:
      {
      TCHAR szBuffer[101];
      static int row = 0;
      static int msg_num = 1;

      		HDC hDC = GetDC(hWnd);
      
      		FillMemory(szBuffer, 100, 32 );
      		TextOut( hDC, 0, row, szBuffer, 100 );
      		wsprintf(szBuffer, "%3d: %s", msg\_num++, (LPSTR)lParam );
      		TextOut(hDC, 0, row, szBuffer, lstrlen(szBuffer) );
              row = (row > 200 ) ? 0 : row += 20;
      		ReleaseDC(hWnd,hDC);
      
      	}
      	break;
      

      Please h

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      coco243 wrote:

      execution stops at WaitForSingleObject, as if hAutoEvent isn't signaled or I don't know what is happening

      coco243 wrote:

      Please help me to find what I am not doing right.

      It's not signaled on the second pass. You are using an Auto-Reset event object. So when your ChildThreadProc thread exits the event goes back into a nonsignaled state. The operating system is managing this for you. Event Objects (Synchronization) - Win32 apps | Microsoft Docs[^] You could change to a manual signal and set/reset it yourself. Best Wishes, -David Delaune

      C 1 Reply Last reply
      0
      • L Lost User

        coco243 wrote:

        execution stops at WaitForSingleObject, as if hAutoEvent isn't signaled or I don't know what is happening

        coco243 wrote:

        Please help me to find what I am not doing right.

        It's not signaled on the second pass. You are using an Auto-Reset event object. So when your ChildThreadProc thread exits the event goes back into a nonsignaled state. The operating system is managing this for you. Event Objects (Synchronization) - Win32 apps | Microsoft Docs[^] You could change to a manual signal and set/reset it yourself. Best Wishes, -David Delaune

        C Offline
        C Offline
        coco243
        wrote on last edited by
        #3

        Thank you, I had made the event with manual reset and I had understood what it was to be understanded. In additon, the main significant thing that I had catched it, it was that when the thread was exiting, even if I was signaled the event before exiting from thread:

        SetEvent(hAutoEvent);

        , the system automaticaly was unsigning the event. Very tricky for me, I woldn't figured out by myself neither in 100 year, so, Thank you very much,

        L 1 Reply Last reply
        0
        • C coco243

          Thank you, I had made the event with manual reset and I had understood what it was to be understanded. In additon, the main significant thing that I had catched it, it was that when the thread was exiting, even if I was signaled the event before exiting from thread:

          SetEvent(hAutoEvent);

          , the system automaticaly was unsigning the event. Very tricky for me, I woldn't figured out by myself neither in 100 year, so, Thank you very much,

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          coco243 wrote:

          I woldn't figured out by myself neither in 100 year

          You are doing an amazing job. I would be completely lost if I were forced to read Romanian documentation. I checked and unfortunately the MSDN isn't available in your native tongue. Event Objects (Romanian)[^]

          C 1 Reply Last reply
          0
          • L Lost User

            coco243 wrote:

            I woldn't figured out by myself neither in 100 year

            You are doing an amazing job. I would be completely lost if I were forced to read Romanian documentation. I checked and unfortunately the MSDN isn't available in your native tongue. Event Objects (Romanian)[^]

            C Offline
            C Offline
            coco243
            wrote on last edited by
            #5

            Thank you, With a little English, with a little will, maybe something came out. Thank you again for your help,

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups