Solved

WaitForSingleObject()

Posted on 2002-07-08
7
2,705 Views
Last Modified: 2008-03-03
Hello Expert,

Have A Thread that in some point calls:
WaitForSingleObject(hEvent, INFINITE);

The event object is created like this:
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

At some other point,
SetEvent(hEvent);
is called.

The problem:
The thread does not response to the "signaling" of hEvent an remains blocked.

Can you think about a reason for this ?

TIA,
Oren.
0
Comment
Question by:orenm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7140293
Well, first obvious questions: are you sure the CreateEvent() call succeeded? What results do you get if you call GetLastError() after the CreateEvent() and SetEvent() calls?
0
 

Author Comment

by:orenm
ID: 7140403
The CreateEvent() and SetEvent() calls are successful:
GetLastError() returns with 0 and the message:
"The operation completed successfully."

Oren.
0
 
LVL 10

Expert Comment

by:makerp
ID: 7140475
try

CreateEvent(NULL,TRUE,FALSE,NULL)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
weicco earned 100 total points
ID: 7140979
How are threads created and how the event handle is passed to threads?

If you create new thread and doesn't enable handle inheritance or you created event without handle inheritance, new thread doesn't get handle to event.

But if you use global variable to store and access event handle this shouldn't be the problem.
0
 

Author Comment

by:orenm
ID: 7142349
Some more information:

Same process (no security inheritance needed).
The event handles are static/global and not passed to the threads.

The application creates 2 identical threads like this:
m_hThread = (HANDLE)_beginthreadex(0, 0, ..., ..., 0, 0);

This should be interesting:
The WaitForSingleObject() DOES continue in case that SetEvent() is called from the "twin" thread.
The original problem is true in case that SetEvent() is called by the "main" thread.

hope this new information will help to make conclusions.

Oren.

P.S.
Actually this threads code is embedded in some C++ classes but this doesn't look significant.
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7142686
Er...just a second, if the main thread is blocking on a WaitForSingleObject() call, how is it ever going to reach the code which calls SetEvent()?
0
 

Author Comment

by:orenm
ID: 7142771
Well, the bug is found.

for the interested and the familiar with C++:
The event handler was created in a constructor which actually invoked more then once for some objects, so,
the WaitForSingleObject() and SetEvent() had the same name of event handler, but,
actually relating to different handlers.

Thanks any way.

weicco, you were the closest, so you get the points.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question