event monitor and thread sleep

Posted on 2006-04-06
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
Question by:dech
    LVL 48

    Expert Comment

    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.
    LVL 2

    Author Comment

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

    Expert Comment

    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?
    LVL 21

    Expert Comment

    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.

    LVL 2

    Author Comment

    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?
    LVL 48

    Accepted Solution

    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.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now