Events are one of the synchronization objects provided by the Windows CE kernel. Events are used to allow one thread to signal one or more threads that something happened. Examples:
·         Events are used by the kernel and drivers to handle system interrupts. When an interrupt occurs the kernel signals the driver by setting an event that the driver waits for.
·         Events are sometimes used to signal driver or services that registry settings have been changed by an application or Control Panel Applet. The driver waits for the event and then re-reads the registry settings.
Events are initialized by calling CreateEvent(). The signature of CreateEvent is:
HANDLE CreateEvent(
 LPSECURITY_ATTRIBUTES lpEventAttributes,
 BOOL bManualReset,
 BOOL bInitialState,
 LPTSTR lpName
);
This means that there are some options to deal with when creating an event. They are:
  1. lpEventAttributes – this is not used by Windows CE events, so pass in NULL.
  2. bManualReset – a flag to indicate that the event should be a manual reset event or an automatic reset event. A manual reset event requires that the event be set to non-signaled by a call to ResetEvent() when it is signaled by a call to SetEvent(). An automatic reset event will become non-signaled when the first thread waiting for the event is unblocked by the scheduler.
  3. bInitialState – a Boolean that indicates that the event should be signaled or not when it is created.
  4. lpName – an event can either be named using a string value or unnamed by passing in NULL.
Events are either signaled or non-signaled. When a thread waits for an event, the scheduler stops execution of the thread until the event is signaled or the wait times out.  A thread waits for an event by calling either WaitForSingleObject() or WaitForMultipleObjects(). Another thread signals the event by calling either SetEvent() or PulseEvent().
NOTE: When waiting for an event passed to the kernel using InitializeInterrupt(), WaitForSingleObject() MUST be used. WaitForMultipleObjects() is documented to not work, and trust me I made that mistake once – it does not work.
When SetEvent() is called, the event is signaled and:
  • If the event is a manual reset event, all threads waiting for the event are unblocked, or allowed to run. The event remains signaled until ResetEvent() is called.
  • If the event is an automatic reset event and one or more threads are waiting for the event, the first thread waiting for the event is unblocked and then the event is reset to non-signaled. In this case only one thread will be unblocked.
  • If the event is an automatic reset event and no threads are waiting for the event, the event will remain signaled until a thread waits on the event, then it will be reset to non-signaled.
When PulseEvent() is called, the event is signaled and:
  • If the event is a manual reset event, all threads waiting on the event are unblocked and then the event will be automatically reset to non-signaled.
  • If the event is a manual reset event, and no threads are waiting on the event, the event will be reset to non-signaled so no threads are unblocked.
  • If the event is an automatic reset event and one or more threads are waiting for the event, the first thread waiting for the event is unblocked and then the event is reset to non-signaled. In this case only one thread will be unblocked.
  • If the event is an automatic reset event and no threads are waiting for the event, the event will be reset to non-signaled so no threads are unblocked.
  • Note, contrary to the help topic for PulseEvent(), Windows CE does not support waiting for all objects to be signaled.

Events can be named, or unnamed. If an event is created by a single call to CreateEvent() and then the HANLDE used by multiple threads it can be unnamed, but it can be named as well. If multiple processes need to create the event by calling CreateEvent(), then the event must be named. When using a named event, the first call to CreateEvent(), will in fact create the event, subsequent calls will receive a handle to the existing event.

See Windows CE: Event Test Code for sample code demonstrating use of events.

Copyright © 2009 – Bruce Eitman
All Rights Reserved