stuartmclark
stuartmclark

Reputation: 1243

Why does my Windows Service automatically quit after approx 5 secs?

I've create a Windows Service using C# that when OnStart is called creates a new thread from another class. This new thread then loops waiting for any incoming TCP connections. Or it should. When I start the service it automatically stops after about 5 seconds. I don't know why it is doing this. I understand that services close on their own if they have no work to do but the work has been specified for it. Anyone got any ideas why this would be happening? My OnStart Method looks like this:

protected override void OnStart(string[] args)
    {
        Thread thread = new Thread(new StateMachine().AcceptConnections);
        thread.Start();      
    }

Which then calls this method:

        Int32 port = 13000;
        IPAddress localAddr = IPAddress.Parse("127.0.0.1");

        server = new TcpListener(localAddr, port);

        // Start listening for client requests.
        server.Start();
        // Enter the listening loop.
        do
        {
            client = server.AcceptTcpClient();
            ReceivedData();                
        } while (true);
    }

It does not stay on long enough to allow any clients to connect to the TcpListner.

Help?

Upvotes: 2

Views: 2881

Answers (3)

Jaco Pretorius
Jaco Pretorius

Reputation: 24840

Firstly, are you sure you want your service to spawn a new thread to do this? You can't simply do it on the main thread?

To debug your problem, do one or both of the following:

  1. Add some logging, either using the EventLog or a logging framework (Log4Net).
  2. Run your service as a console app and debug it in Visual Studio

Upvotes: 0

stuartmclark
stuartmclark

Reputation: 1243

Right I've got it. I was doing something stupid with the config file, which including moving the .exeXML config file to the wrong place. Which resulted in an Configuration Error, and the service wasn't liking it. I've got it up and running now though. So thanks for your help.

Upvotes: 0

Steve Townsend
Steve Townsend

Reputation: 54128

First, make sure you app code runs OK when not set up as a service. If your code runs OK when not set up as a service, my guess is that your OnStart is taking too long for some reason. If you cannot speed it up, the info here allows you to handle necessary startup latency.

In Win32, you have to update the service status by notifying the Service Control Manager (SCM) of startup (and stop) progress periodically, or it will think your service is hung and kill it. You may need to do this only once, or on a timer if your initialization takes a long time. While your startup (stop) logic is in progress, you tell SCM you are in state START_PENDING (STOP_PENDING) and once it's done you tell it you are in STARTED (STOPPED) state.

In the managed world, this goal is achieved by calling ServiceBase.RequestAdditionalTime. There is a thorough overview of this topic here. The money quote:

This is where your managed service needs to pay attention to avoid the SCM flagging your service as unresponsive.

  • You don’t have to explicitly update the state; ServiceBase does this for you when your OnStart or OnStop method completes
  • You do have to call RequestAdditionalTime if you expect the OnX method to exceed the timeout.

Upvotes: 2

Related Questions