0
votes

I've created a Windows Service in C# that installs fine, but when I attempt to run it I get a 'Error 1053: The service did not respond to the start or control request in a timely fashion.'. I'm new to windows services, and am uncertain as to why this would be occurring. Code below for the service:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Messaging;
using System.Threading;

namespace DataStream
{
    public partial class DataStreamService : ServiceBase
    {
        private EventLog logger;

        public DataStreamService()
        {
            this.AutoLog = false;
            if (!System.Diagnostics.EventLog.SourceExists("DataStream"))
            {
                EventLog.CreateEventSource("DataStream", "DataStreamLog");
            }
            logger = new EventLog();
            logger.Source = "DataStream";
        }

        protected override void OnStart(string[] args)
        {
            logger.WriteEntry("*-Service Started-*");
            Thread workThread = new Thread(this.moveTheQueue);
            workThread.Start();

        }

        private void moveTheQueue(object data)
        {
            logger.WriteEntry("*-Connecting to Queue-*");
            if (MessageQueue.Exists(@"./Private/testqueue"))
            {
                MessageQueue queue = new MessageQueue(@"./Private/testqueue");
                MessageEnumerator enumerator = queue.GetMessageEnumerator2();
                Message msg;

                while (enumerator.MoveNext(new TimeSpan(0, 0, 20)))
                {
                    try
                    {
                        msg = queue.PeekById(enumerator.Current.Id);
                        System.IO.File.WriteAllText(@"C:\Users\Public\MSMQTest\" + msg.Label + enumerator.Current.Id + "_" + DateTime.Now.ToString() + ".xml", msg.Body.ToString());
                        queue.ReceiveById(enumerator.Current.Id);
                        logger.WriteEntry("*-Received Message with Id " + msg.Id + " and Label " + msg.Label);
                    }
                    catch (Exception e)
                    {
                        logger.WriteEntry("*-ERROR: Failed to Obtain Message with Id " + enumerator.Current.Id);
                    }
                }
            }
            else
            {
                logger.WriteEntry("*-ERROR: No queue found. Check user credentials.-*");
            }
            Stop();
        }

        protected override void OnStop()
        {
            EventLog.WriteEntry("*-Service Stopped-*");
        }
    }
}
2
Well, balls, you're already doing it. Pull the code from the constructor and see if it starts up faster.user1228
@Will That sped it up enough. Post as answer with a small explanation and I'll accept.steventnorris
What happens if you throw an exception in the constructor? Does it fail in the same manner?Chris

2 Answers

2
votes

No need for a code dump.

When services are started by the system, they only have a short amount of time to return from the OnStart method before you get that message.

The solution is to move all computationally intense work onto a different thread.

... Of course, you're already doing that. Great. But you're also doing work in your constructor, which either takes too long or may even fail. So pull that work out of the constructor and put it in your background thread as well.

0
votes

I've encountered a similar issue. My experience with this was during the startup of a service on reboot. I had two services trying to start at the same time using the same resources. One would fail and the other would start.

On the setup option for the service I set it for a delayed start on the failing service. Upon reboot the system worked and both services ran.