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-*");
}
}
}