Click here to Skip to main content
15,885,767 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Complier error C2280 explanation Pin
ForNow5-May-22 2:07
ForNow5-May-22 2:07 
GeneralRe: Complier error C2280 explanation Pin
CPallini5-May-22 2:10
mveCPallini5-May-22 2:10 
GeneralRe: Complier error C2280 explanation Pin
ForNow5-May-22 2:21
ForNow5-May-22 2:21 
GeneralRe: Complier error C2280 explanation Pin
CPallini5-May-22 2:34
mveCPallini5-May-22 2:34 
GeneralRe: Complier error C2280 explanation Pin
ForNow5-May-22 3:01
ForNow5-May-22 3:01 
Questioncannot Create CListBox : CFormView without IDD Pin
rtischer82774-May-22 4:04
rtischer82774-May-22 4:04 
QuestionRe: cannot Create CListBox : CFormView without IDD Pin
David Crow4-May-22 4:47
David Crow4-May-22 4:47 
QuestionWaitForSingleObject Visual C++ bug Pin
coco2431-May-22 8:35
coco2431-May-22 8:35 
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:

C++
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:

C++
// 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:

C++
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:

C++
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 help me to find what I am not doing right.

Thank you,
AnswerRe: WaitForSingleObject Visual C++ bug Pin
Randor 1-May-22 10:22
professional Randor 1-May-22 10:22 
GeneralRe: WaitForSingleObject Visual C++ bug Pin
coco2434-May-22 5:43
coco2434-May-22 5:43 
GeneralRe: WaitForSingleObject Visual C++ bug Pin
Randor 4-May-22 8:02
professional Randor 4-May-22 8:02 
GeneralRe: WaitForSingleObject Visual C++ bug Pin
coco2434-May-22 9:19
coco2434-May-22 9:19 
QuestionOne DrawItem Function for both CStatic and CComboBox Pin
ForNow30-Apr-22 15:11
ForNow30-Apr-22 15:11 
AnswerRe: One DrawItem Function for both CStatic and CComboBox Pin
Richard MacCutchan30-Apr-22 21:06
mveRichard MacCutchan30-Apr-22 21:06 
GeneralRe: One DrawItem Function for both CStatic and CComboBox Pin
ForNow1-May-22 3:27
ForNow1-May-22 3:27 
GeneralRe: One DrawItem Function for both CStatic and CComboBox Pin
Richard MacCutchan1-May-22 3:36
mveRichard MacCutchan1-May-22 3:36 
GeneralRe: One DrawItem Function for both CStatic and CComboBox Pin
ForNow1-May-22 3:52
ForNow1-May-22 3:52 
QuestionClist Find question Pin
ForNow29-Apr-22 3:12
ForNow29-Apr-22 3:12 
AnswerRe: Clist Find question Pin
Richard Andrew x6429-Apr-22 3:34
professionalRichard Andrew x6429-Apr-22 3:34 
GeneralRe: Clist Find question Pin
ForNow29-Apr-22 3:58
ForNow29-Apr-22 3:58 
GeneralRe: Clist Find question Pin
Richard Andrew x6429-Apr-22 4:02
professionalRichard Andrew x6429-Apr-22 4:02 
GeneralRe: Clist Find question Pin
ForNow29-Apr-22 4:15
ForNow29-Apr-22 4:15 
GeneralRe: Clist Find question Pin
Richard Andrew x6429-Apr-22 4:17
professionalRichard Andrew x6429-Apr-22 4:17 
Questionvector, adding things Pin
Calin Negru25-Apr-22 18:42
Calin Negru25-Apr-22 18:42 
AnswerRe: vector, adding things Pin
CPallini25-Apr-22 20:21
mveCPallini25-Apr-22 20:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.