My application needs to review all new application Event Log entries as they come in.
private void eventLog_Application_EntryWritten(object sender, EntryWrittenEventArgs e)
{
// Process e.Entry
}
What I would like to know is what happens if another Entry is written to the EventLog while a previous Entry is being handled?
The documentation for EventLog.EntryWritten Event provides an example of handling an entry written event which uses threading (which is why I am asking the question).
In this example they use System.Threading
and call the WaitOne()
and Set()
methods on the AutoResetEvent
class, however I'm not sure precisely what this code is intended to achieve.
The documentation states that - WaitOne()
"blocks the current thread until the current WaitHandle
receives a signal", and that Set()
"sets the state of the event to signaled, allowing one or more waiting threads to proceed". I'm not sure what the threading portion of this example is intended to demonstrate, and how this relates to how (or if) it needs to be applied in practice.
It appears that WaitOne()
blocks the thread immediately after the entry has been written, until it has been handled, where it is then set to signalled (using Set()
), before allowing the thread to proceed. Is this the one and only thread for the application?
Most importantly, when my application is not responsible for writing the the events which need to be read from the EventLog, how should this principle be applied? (If, indeed, it needs to be applied.)
What does happen if a new Entry
is written while the application is inside the handler?
obj
is the variable we want to use):object lock; while (obj.IsLocked) { /* wait by doing nothing */ } if ((lock = obj.Lock()) == null) { /* failed */ } else { /* log */ obj.Unlock(lock); }
– DiscipleEventLog
) – Embraceor