Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Service to run constantly

I've created a Windows Service called ProxyMonitor and I'm currently at the stage where the service is installs and uninstall's the way I want it.

So I execute the application like so:

C:\\Windows\\Vendor\\ProxyMonitor.exe /install 

Pretty self explanatory, and then I got to services.msc and and start the service, but when I do this I get the following message:

The Proxy Monitor Service on Local Computer started and then stopped. Some services stop automatically if there is no work to do, For example, The performance Logs and Alerts Services

My code looks like so:

public static Main(string[] Args) {     if (System.Environment.UserInteractive)     {         /*             * Here I have my install logic         */     }     else     {         ServiceBase.Run(new ProxyMonitor());     } } 

And then within ProxyMonitor class I have:

public ProxyMonitor() { }  protected override void OnStart(string[] args) {     base.OnStart(args);     ProxyEventLog.WriteEntry("ProxyMonitor Started");      running = true;     while (running)     {         //Execution Loop     } } 

and onStop() I just change the running variable to false;

What would I need to do to make the Service constantly active, as I would need to be monitoring the network I need to trace changes etc.


Update: 1

protected override void OnStart(string[] args) {      base.OnStart(args);      ProxyEventLog.WriteEntry("ProxyMonitor Started");       Thread = new Thread(ThreadWorker);      Thread.Start();  } 

Within the ThreadWorker I have ProxyEventLogger.WriteEntry("Main thread entered") which does not get fired.

like image 954
RobertPitt Avatar asked Feb 01 '11 15:02

RobertPitt


People also ask

Does a Windows service run continuously?

Once the win service is started then this process will run continuously in background. I rally need the code for after OnStart event. If I call GetEmployees method on OnStart then this event is not completing because I am calling GetEmployees method again once the process is completed.

How do I run a Windows service once a day?

Just create a regular console app, then use the Windows scheduler to run your program at 6am. A service is when you need your program to run all the time.

How often does a Windows service run?

Definition of Windows Services Unlike regular software that is launched by the end user and only runs when the user is logged on, Windows Services can start without user intervention and may continue to run long after the user has logged off.

What does it mean to run something as a Windows service?

A Sitecore Host application can run as a Windows Service. A Windows Service is an executable application that the operating system runs in the background. It does not require a logged-in user session to run. In Windows, the Service Control Manager (SCM) manages all Windows service processes.


1 Answers

The OnStart() callback needs to return in a timely fashion, so you'll want to kick off a thread where all your work will be performed. I would recommend adding the following fields to your class:

using System.Threading; private ManualResetEvent _shutdownEvent = new ManualResetEvent(false); private Thread _thread; 

The _thread field will hold a reference to the System.Threading.Thread object you create in the OnStart() callback. The _shutdownEvent field holds a system-level event construct that will be used to signal the thread to stop running on service shutdown.

In the OnStart() callback, create and start your thread.

protected override void OnStart(string[] args) {      _thread = new Thread(WorkerThreadFunc);      _thread.Name = "My Worker Thread";      _thread.IsBackground = true;      _thread.Start(); } 

You need a function named WorkerThreadFunc in order for this to work. It has to match the System.Threading.ThreadStart delegate signature.

private void WorkerThreadFunc() { } 

If you don't put anything in this function, the thread will start up and then immediately shutdown, so you have to put some logic in there that basically keeps the thread alive while you do your work. This is where the _shutdownEvent comes in handy.

private void WorkerThreadFunc() {     while (!_shutdownEvent.WaitOne(0)) {         // Replace the Sleep() call with the work you need to do         Thread.Sleep(1000);     } } 

The while loop checks the ManualResetEvent to see if it is "set" or not. Since we initialized the object with false above, this check returns false. Inside the loop, we sleep for 1 second. You'll want to replace this with the work you need to do - monitor proxy settings, etc.

Finally, in the OnStop() callback of your Windows Service, you want to signal the thread to stop running. This is easy using the _shutdownEvent.

protected override void OnStop() {      _shutdownEvent.Set();      if (!_thread.Join(3000)) { // give the thread 3 seconds to stop          _thread.Abort();      } }  

Hope this helps.

like image 120
Matt Davis Avatar answered Oct 14 '22 07:10

Matt Davis