Click here to Skip to main content
15,889,723 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is the purpose of ResetEvent(), While we can complete the task by using CreateEvent(),SetEvent() & WaitForSingleObject() functions?
Posted
Comments
Sergey Alexandrovich Kryukov 1-Jul-11 0:22am    
It looks like you thinks you known why using SetEvent.
In this case, I do not understand, how it's possible to not understand why using ResetEvent.
--SA

Google is your friend, use it well.

http://msdn.microsoft.com/en-us/library/ms685081(v=vs.85).aspx[^]

According to MSDN

The ResetEvent function is used primarily for manual-reset event objects, which must be set explicitly to the nonsignaled state. Auto-reset event objects automatically change from signaled to nonsignaled after a single waiting thread is released.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Jul-11 0:25am    
That's correct, my 5.
--SA
ResetEvent() is for 'un'SetEvent()'ing

In your use case you're obviously using the event singularly, but there are cases where the WaitForSingleObject() is within a loop and will be called again, normally in a job workerthread scenario (although PulseEvent() may be more appropriate sometimes)

main thread
// pseudo code
for(int loop=0;looper<10;looper++)
{
// add job
AddJobToWorkerThread();
// signal a new job is to be done
SetEvent(newJobToBeDoneEvent);
// and sleep
Sleep(10000);
}


workerthread
// 
while(!threadToBeTerminated)
{
if(WaitForSingleObject(newJobToBeDoneEvent,5000)==WAIT_OBJECT_0)
{
// do the job we were posted
...
// then clear the flag and wait again
ResetEvent(newJobToBeDoneEvent);
}
else
   break;
}
 
Share this answer
 
Comments
Chuck O'Toole 30-Jun-11 1:21am    
Microsoft recommends not using PulseEvent().

From MSDN

PulseEvent Function
Sets the specified event object to the signaled state and then resets it to the nonsignaled state after releasing the appropriate number of waiting threads.

Note This function is unreliable and should not be used. It exists mainly for backward compatibility. For more information, see Remarks.
barneyman 30-Jun-11 1:29am    
you learn something new every day - thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900