event monitor and thread sleep

Hi,

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
LVL 2
dechAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
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.
dechAuthor Commented:
It checks the event type and fires SQL stored proc if matched
AlexFMCommented:
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?
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

mastooCommented:
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)
        signal.WaitOne()
    End Sub ' Main


    Public Shared Sub MyOnEntryWritten(ByVal [source] As Object, ByVal e As EntryWrittenEventArgs)
        signal.Set()
    End Sub ' MyOnEntryWritten
End Class ' MySample
[C#]
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);
   signal.WaitOne();        

    }      

    public static void MyOnEntryWritten(object source, EntryWrittenEventArgs e){
        signal.Set();
    }
}
[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.

dechAuthor Commented:
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?
AlexFMCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.