We help IT Professionals succeed at work.

event monitor and thread sleep

dech asked
Medium Priority
Last Modified: 2010-04-16

I wrote a little console application which monitors the windows event log for new entries.

In order to get the console app to stay alive I put the thread to sleep.

My question is:

While the thread is asleep, would the event get caught? If so, how ... i.e. does the thread get awoken or is the event done on a seperate thread, or even the events will get queued up until the thread is awake again.

Thanks for help
Watch Question

I guess you are talking about EventLog class. It must have it's own thread and invoke client's event handlers in the context of this thread. This means, event handlers are invoked immediately even when main thread sleeps.
What does your program do after creating EventLog and subscribing to events? What does it do when log is changed? I am not sure that console application is appropriate type for this task.


It checks the event type and fires SQL stored proc if matched

If this is Console application, you need to find the way to wait for user input, for example:
Press any key to continue...
and after pressing key exit. All time when it is active, it monitors Event Log. I don't know how this can be implemented in the Console application. Maybe you need Windows Service or Windows application which adds icon to system tray and allows to select "Exit" from the context menu. But main question is: how do you want this application to look like? Should it have user interaction, how should it be started and stopped?

You could either create a thread to handle the events and come up with some "stop" mechanism for the main thread, or just do something like the example from help below (excuse the vb but you get the idea).  No reason it wouldn't work in a console.  As AlexFM mentions, a service might be more appropriate in which case your code is threaded anyway.

Class MySample

    ' This member is used to wait for events.
    Private Shared signal As AutoResetEvent

    Public Shared Sub Main()

        Dim myNewLog As New EventLog()
        myNewLog.Log = "MyCustomLog"

        AddHandler myNewLog.EntryWritten, AddressOf MyOnEntryWritten
        myNewLog.EnableRaisingEvents = True

        signal = New AutoResetEvent(False)
    End Sub ' Main

    Public Shared Sub MyOnEntryWritten(ByVal [source] As Object, ByVal e As EntryWrittenEventArgs)
    End Sub ' MyOnEntryWritten
End Class ' MySample
using System;
using System.Diagnostics;
using System.Threading;
class MySample{

    // This member is used to wait for events.
    static AutoResetEvent signal;

    public static void Main(){
        EventLog myNewLog = new EventLog();
        myNewLog.Log = "MyCustomLog";                      
        myNewLog.EntryWritten += new EntryWrittenEventHandler(MyOnEntryWritten);
        myNewLog.EnableRaisingEvents = true;
   signal = new AutoResetEvent(false);


    public static void MyOnEntryWritten(object source, EntryWrittenEventArgs e){
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button  in the upper-left corner of the page.


Through experimentations ... it seems the thread gets awoken by events (I put the thread to sleep for hours, but events still fire).

Does this means that event listeners are run on their own threads?
Yes, events are actually function pointers, event source calls event handlers directly from his own thread. You can check this printing Thread.CurrentThread.ManagedThreadId or Thread.CurrentThread.GetHashCode from main thread and event handler and compare them.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.