Goober
Goober

Reputation: 13506

C# Windows Service

Scenario

I've created a windows service, but whenever I start it, it stops immediately. The service was concieved from a console application that used to subscribe to an event and watch processes on a server. If anything happened to process (i.e. It was killed), then the event would trigger the process to be restarted. The reason I'm telling you this is because the original code used to look like this:

Original Console App Code:

    static void Main(string[] args)
    {
        StartProcess sp = new StartProcess();
        //Note the readline that means the application sits here waiting for an event!
        Console.ReadLine(); 
    }

Now that this code has been turned into a Windows Service, it is essentially EXACTLY THE SAME. However, the service does not sit there waiting, even with the readline, it just ends.....

New Windows Service Code:

    protected override void OnStart(string[] args)
    {
        ProcessMonitor pm = new ProcessMonitor();
        Console.ReadLine();
    }

Thoughts

Since the functionality is entirely encapsulated within this single class (It quite literally starts, sets up some events and waits) - How can I get the service to actually sit there and just wait? It seems to be ignoring the readline. However this works perfectly as a console application, it is just far more convenient to have it as a service.

Upvotes: 0

Views: 2428

Answers (5)

Gabriel Worley
Gabriel Worley

Reputation: 66

Move your Console.ReadLine(); into your ProcessMonitor() constructor, and create your ProcessMonitor inside the constructor for the service. Your OnStart method can be empty. Despite what people are saying the Console methods will NOT crash your service, however it is probably not best practice. I guess the proper way to keep a service running (after your timers are started) is to use a while loop with a Thread.Sleep(60000) inside it.

When I am writing a service I put all the functionality in a Class Library project, then I create a Console Application project to test the service functionality, and then a Windows Service project. Both the Console Application and Windows Service project call one method in the Class Library to start the service functionality. If you use this technique you can call Console.WriteLine in the Class Library which can be viewed when running the Console Application. PS Topshelf is overrated, writing a windows service is not that difficult.

public partial class ProcessMonitor_Service : ServiceBase
{

    public ProcessMonitor_Service()
    {
        InitializeComponent();            
        ProcessMonitor pm = new ProcessMonitor();
    }

    protected override void OnStart(string[] args)
    {

    }

    protected override void OnStop()
    {

    }
}

public class ProcessMonitor
{
    public ProcessMonitor()
    {
        // start timers
        Console.ReadLine(); 
    }
}

Upvotes: 0

Lucas B
Lucas B

Reputation: 12013

OnStart() must complete and end successfully for the service to be considered "Started"

Upvotes: 0

ChaosPandion
ChaosPandion

Reputation: 78282

Typically you would want something like this. As Joe mentioned in the comments you want Start to initialize and release control to another thread to make sure that you return within 30 seconds.

private readonly ProcessMonitor processMonitor = new ProcessMonitor();

protected override void OnStart(string[] args)
{
    processMonitor.Start();
}

protected override void OnStop()
{
    processMonitor.Stop();
}

Upvotes: 5

ChrisLively
ChrisLively

Reputation: 88082

Well... A service doesn't have a console input/output. So the ReadLine won't stop it from executing.

What does ProcessMonitor do?

Typically, for services your code lives in a thread that monitors whether the service has been stopped or paused.

Upvotes: 2

AlG
AlG

Reputation: 15167

In a Service there is no concept of a readline - there's no keyboard. I wouldn't be surprised if this is throwing an exception at that call. Have you checked your Application Log?

Upvotes: 4

Related Questions