-
Jul 29th, 2020, 02:14 PM
#1
Thread Starter
Lively Member
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
-
Jul 29th, 2020, 02:21 PM
#2
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?
-
Jul 29th, 2020, 02:42 PM
#3
Thread Starter
Lively Member
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.
-
Jul 29th, 2020, 03:32 PM
#4
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
-
Jul 29th, 2020, 07:17 PM
#5
Thread Starter
Lively Member
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...
-
Jul 29th, 2020, 08:36 PM
#6
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
-
Jul 30th, 2020, 03:27 AM
#7
Re: Catching an event in a serice
Originally Posted by groston
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|