?
Solved

Raising events inside a Windows Service. More points follow.

Posted on 2006-03-27
15
Medium Priority
?
312 Views
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.
0
Comment
Question by:broadbent
  • 9
  • 6
15 Comments
 
LVL 4

Expert Comment

by:sr101880
ID: 16302152
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. :-)
0
 
LVL 4

Author Comment

by:broadbent
ID: 16303471
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"
0
 
LVL 4

Expert Comment

by:sr101880
ID: 16310879
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Author Comment

by:broadbent
ID: 16311733
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.

0
 
LVL 4

Expert Comment

by:sr101880
ID: 16312059
Can you post your code for your client app that is listening?
0
 
LVL 4

Author Comment

by:broadbent
ID: 16312347
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.
0
 
LVL 4

Expert Comment

by:sr101880
ID: 16321389
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!
0
 
LVL 4

Accepted Solution

by:
sr101880 earned 200 total points
ID: 16321414
I forgot to mention:  Swap "Alerter"  for your service name.  :-)
0
 
LVL 4

Author Comment

by:broadbent
ID: 16330628
Doesn't this code rely on the listener implementing a timer to check if the service is paused?
0
 
LVL 4

Expert Comment

by:sr101880
ID: 16342090
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.  
0
 
LVL 4

Author Comment

by:broadbent
ID: 16342221
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.
0
 
LVL 4

Expert Comment

by:sr101880
ID: 16342307
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

0
 
LVL 4

Author Comment

by:broadbent
ID: 16342571
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.
0
 
LVL 4

Expert Comment

by:sr101880
ID: 16342625
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.  :-)
0
 
LVL 4

Expert Comment

by:sr101880
ID: 16342652
A new question post might be a more productive way to get the answers you want because more people will get involved.
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question