Raising events inside a Windows Service. More points follow.

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.
LVL 4
broadbentAsked:
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.

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

http://www.vbdotnetheaven.com/Uploadfile/mgold/ApplicationScheduler11112005001112AM/ApplicationScheduler.aspx?ArticleID=7f292e7f-10cd-4577-93f3-8a0d63dfe2e9

They use a service to call a list of apps from an xml file. :-)
broadbentAuthor Commented:
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"
sr101880Commented:
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.


http://www.codeproject.com/dotnet/simplewindowsservice.asp

http://www.devcity.net/Articles/190/3/article.aspx

http://www.desaware.com/tech/remoteevents.aspx

http://www.devx.com/dotnet/Article/7914
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

broadbentAuthor Commented:
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.

sr101880Commented:
Can you post your code for your client app that is listening?
broadbentAuthor Commented:
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.
sr101880Commented:
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.

            Try
                ' 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!
            Catch
                Console.WriteLine("Could not start the Alerter service.")  '<-----Create a custom message
            End Try
        End If


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

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
broadbentAuthor Commented:
Doesn't this code rely on the listener implementing a timer to check if the service is paused?
sr101880Commented:
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.  
broadbentAuthor Commented:
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.
sr101880Commented:
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

broadbentAuthor Commented:
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.
sr101880Commented:
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.  :-)
sr101880Commented:
A new question post might be a more productive way to get the answers you want because more people will get involved.
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
Visual Basic.NET

From novice to tech pro — start learning today.