C# Events at same time ignored

放肆的年华 提交于 2020-01-16 18:31:42

问题


I am currently running into a problem with an eventlog monitor I created a while ago.

My application subscribes to the EntryWritten events of the EventLog. I noticed that if multiple events occurs at the same time (within the same second), only one of them raises the event which triggers my eventhandler.

_eventLog = new System.Diagnostics.EventLog(_logName);
_eventLog.EntryWritten += new EntryWrittenEventHandler(eventlog_EntryWritten);
_eventLog.EnableRaisingEvents = true;

Is it possible to avoid this behavior, so my eventlog_EntryWritten method will be called for all events that are logged to the eventlog?


回答1:


Events usually dont run in Async mode, and so if eventlog_EntryWritten does not finish before another event, you probably miss the events. Raise your own async event in fire and forget method. I am not sure, but probably this will be more effective. Or have your own class using polling mechanism to read the events.




回答2:


It's worse than that - it's within a five second interval:

The system responds to WriteEntry only if the last write event occurred at least five seconds previously. This implies you will only receive one EntryWritten event notification within a five-second interval, even if more than one event log change occurs.

(from EventLog.EntryWritten)

If you need to track all events then (if they're yours), I'd suggest adding a wrapper function around WriteEntry that can catch all of the entries before they're even written to the log.



来源:https://stackoverflow.com/questions/4816642/c-sharp-events-at-same-time-ignored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!