Results 1 to 7 of 7

Thread: Catching an event in a serice

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    103

    Question Catching an event in a serice

    I am trying to write a (simple) service that does something when the computer awakens. To make sure that things are heading in the right direction, I started by first writing a simple service [basically, it just writes a string to the event log]. That worked!

    Then, I tried to modify the code so that it would write a string to the event log each time a mouse button was clicked within the past 10 seconds. I figured that to detect the mouse click, I should look for the event as this would be, programmatically, quite similar to the desired goal. Sadly, this did not work.

    Following is the code - please tell me what to add to make it work, i.e., please provide the code that should be in the MyTickHandler function. Thanks!

    Code:
    Imports System.Diagnostics  
    Imports System.Timers  
      
    Public Class MyTestService  
        Private Timer1 As New Timer With {.Interval = 1000}  
      
        Public Sub New()  
            MyBase.New()  
            InitializeComponent()  
      
            ' Set up event log  
            Me.EventLog1 = New System.Diagnostics.EventLog  
            EventLog1.Source = "MyTestSource"  
            EventLog1.Log = "MyTestNewLog"  
      
            If Not System.Diagnostics.EventLog.SourceExists(EventLog1.Source) Then  
                System.Diagnostics.EventLog.CreateEventSource(EventLog1.Source, EventLog1.Log)  
            End If 
    
            AddHandler Timer1.Elapsed, New ElapsedEventHandler(AddressOf MyTickHandler)  
            Timer1.Start()  
        End Sub  
      
        Protected Overrides Sub OnStart(ByVal args() As String)  
            EventLog1.WriteEntry("In OnStart")  
        End Sub  
      
        Protected Overrides Sub OnStop()  
            EventLog1.WriteEntry("In OnStop.")  
        End Sub  
      
        Sub MyTickHandler(ByVal sender As Object, ByVal e As EventArgs)  
            ' if mouse is clicked, then call: EventLog1.WriteEntry("MouseClick")   
        End Sub  
    End Class

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Catching an event in a serice

    Not an answer to your question, but why are you importing System.Diagnostics and then writing a full reference to stuff such as System.Diagnostics.EventLog? And why do you want to register mouse clicks like that?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    103

    Re: Catching an event in a serice

    Peter,

    - "...why are you importing System.Diagnostics..." - This code was copied from multiple source and perhaps this is not needed. Not the key issue (but one worth looking at!).
    - "...why do you want to register mouse clicks..." - As I stated, I am using the mouse clicks as a 'replacement' for power events since they are much easier/faster for testing than the actual power events.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: Catching an event in a serice

    This is something running in the background, right?

    Mouse clicks may be easier/faster, but I'm not sure that they are a good stand-in for a power event. Mouse clicks would be system messages, and you can intercept those. However, I'm not sure that a power event would raise the same kind of event. Some of them (oops, I pulled out the plug) won't have any event at all, while others would have some kind of event, but not a user input event like a mouse click.

    On the other hand, things have changed over the years. There may well be system power events that exist these days.

    You might find this link talks a bit about what I'm getting at:

    https://www.codeproject.com/Tips/559...g-managed-code

    The point is that you can hook mouse events from a process, but that's not what you are talking about, and likely doesn't represent a valid stand in for what you are really trying to do.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    103

    Re: Catching an event in a serice

    Without boring you with the details of why... What I want to do is write a program, probably has to be a service, that will display a message (randomly taken from a file) each time the user logs in or the computer awakens from standby mode. Seems fairly simple, but this is mostly new to me...

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: Catching an event in a serice

    Yeah, but THAT's what you want to do. I don't think that catching mouse events is a good stand in. Most mouse events are user actions that go straight to a program. You're talking about power actions. Perhaps they are the same, but I'm thinking they might not be.
    My usual boring signature: Nothing

  7. #7
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Catching an event in a serice

    Quote Originally Posted by groston View Post
    Peter,

    - "...why are you importing System.Diagnostics..." - This code was copied from multiple source and perhaps this is not needed. Not the key issue (but one worth looking at!).
    - "...why do you want to register mouse clicks..." - As I stated, I am using the mouse clicks as a 'replacement' for power events since they are much easier/faster for testing than the actual power events.
    UI interactions and services are not the easiest things to get working together, the service will typically be running as a different user to the currently logged in user and might not even have a desktop session that can generate mouse clicks anyway.

    If you are wanting to detect power events it might be easiest to just go ahead and try that - mouse clicks could be an unnecessary complication here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width