Solved

WaitForSingleObject()

Posted on 2002-07-08
7
2,687 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
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now