Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

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
0
dech
Asked:
dech
  • 3
  • 2
1 Solution
 
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.
0
 
dechAuthor Commented:
It checks the event type and fires SQL stored proc if matched
0
 
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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.

0
 
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?
0
 
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now