WaitForSingleObject Visual C++ bug
-
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
-
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
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
-
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
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,
-
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,
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)[^]
-
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)[^]