We help IT Professionals succeed at work.

Raising events inside a Windows Service. More points follow.

broadbent asked
Medium Priority
Last Modified: 2010-05-18
I have written a windows service, which so far works by beeping the speaker every minute.
However I want to raise events in the timer subroutine, which will trigger my client application to do something.

I've looked everywhere, but can't find an answer.

Q1. Is it possible?

If it is I'll add more points.
Watch Question

Yes, it is possible.  This article can get you started:  


They use a service to call a list of apps from an xml file. :-)


I am afraid the article ( if I've read it right ) doesn't help, because what it does is to launch an application at a time stored in an XML file. It doesn't raise an event.

My problem is that the 'client' app which is listening for these events can't reference the exe file which acts as the service - "this is not a valid assembly or COM component"

I wasn’t able to find code to do exactly what you wanted.  I found articles that have proved it is possible.  I have found the following articles to be useful information that will help you on your way to finding a solution.  I am sorry I couldn’t get you any closer to a solution.






I've looked at these links, and thankyou for them. However I have seen all of these except the desaware site, but that requires much money being spent, and even then the documentation doesn't make it clear if it helps. The other sites mention nothing about events.

I found this on the MSDN site
"A service is a long-running executable that does not support a user interface, and which might not run under the logged-on user account. The service can run without any user being logged on to the computer.

By default, services run under the System account, which is not the same as the Administrator account. You cannot change the rights of the System account. Alternatively, you can use a ServiceProcessInstaller to specify a user account under which the service will run."

I am thinking events aren't possible.

Can you post your code for your client app that is listening?


I haven't written any yet!

Actually, there is no client app as such. What I have is a Service app with timertick event, which raises an event.

My main app would like to listen for this event, and I would normally do this by creating a reference in that app to the service app, and then dim withevents obj = new serviceapp, but VS won't add the reference.

I found this code in a msdn document.  I think that you could setup your service to pause when it raises an event.  Then you could use this code in your main app to listen for the pause.

Dim sc As New ServiceProcess.ServiceController("Alerter")

        Console.WriteLine("The Alerter service status is currently set to {0}", sc.Status)   '<------Remove this line.

        If sc.Status = ServiceProcess.ServiceControllerStatus.Stopped Then  '<-------------------Change this to suit your needs!!!
            ' Start the service if the current status is stopped.
            Console.WriteLine("Starting the Alerter service...")  '<-----Remove this too.

                ' Start the service, and wait until its status is "Running".
                sc.Start()  '<----------------------This one too.
                sc.WaitForStatus(ServiceProcess.ServiceControllerStatus.Running)  '<-------------This is the line you want, it waits for anything i.e Pause!!!

                ' Display the current service status.
                Console.WriteLine("The Alerter service status is now set to {0}.", sc.Status) '<-------Fire off yor main app events here!
                Console.WriteLine("Could not start the Alerter service.")  '<-----Create a custom message
            End Try
        End If

Good Luck!
I forgot to mention:  Swap "Alerter"  for your service name.  :-)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Doesn't this code rely on the listener implementing a timer to check if the service is paused?

no, the line sc.WaitForStatus(ServiceProcess.ServiceControllerStatus.Running) or in your case .Paused waits for the service to do the desired task.  The program will not continue until the service reaches that status.  You might want to start another thread for this because your program will not function until that status is reached.  


Yes but if the program as to wait for the service to rach the status, then this is not like raising an event. The idea is for the program to continue executing and then branching to the event when it is raised.

That is why I suggested starting a new thread for that part of your program.  This way you can interupt your main thread when the desired status is reached.  This article will give you a good overview of how to implement Multithreading: http://www.startvbdotnet.com/threading/default.aspx


Thanks for your comments. And since I have already accepted your previous answer, I fell it is a liberty to prolong this conversation. Unless you want to, that is.

I don't know how much more advice I can give on this subject, but if you have questions I don't mind taking a stab at them.  :-)

A new question post might be a more productive way to get the answers you want because more people will get involved.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.