Search code examples
c#windows-services

Windows Service Timeout then gives error 1053


I have developed a C# Windows service to get some datas from DB and process them in an infinite loop. The service was working fine with nothing but loop in it yesterday but today I have finished the development and tried to test it as a Windows service but it keep says Starting and when the green bar is complete it gives me "1053" error. I have checked if there are any logs and my service is inserting logs and even processing Datas but somehow I still get this error.

I have installed the service from my release folder. There is no error on Event Viewer regarding the service. And my service looks like below.

*UPDATE: When I check event viewer I see below messages in a sequence; "Session 1 started", "Ending Session 1" "Machine restart required". I have tried restarting but it didn't make any difference

enter image description here

Program.cs

static class Program
{
    static void Main()
    {
        try
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
            new spService()
            };
            ServiceBase.Run(ServicesToRun);
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry("Application", ex.ToString(), EventLogEntryType.Error);
        }
    }
}

Service1.cs

public partial class spService: ServiceBase
{
    public spService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        SpPushOperation spo = new SpPushOperation();
        spo.StartSpPushOperation();
    }

    protected override void OnStop()
    {
        SpPushOperation spo = new SpPushOperation();
        spo.StopSpPushOperation();
    }
}

SpPushOperation.cs

class SpPushOperation
{
    public readonly NLog.ILogger Logger = NLog.LogManager.GetCurrentClassLogger();
    public void StartSpPushOperation()
    {
        try
        {
            Logger.Info("-------------");
            Logger.Info("SpPushOperation Started..");
            Logger.Info("-------------");
            while(true)
            {
                //some process in here
            }
        }
        catch(Exception e)
        {

            Logger.Info("!!!!!!!!!!!!!!");
            Logger.Info("Error on getting StartSpPushOperation Error: " + e);
            Logger.Info("!!!!!!!!!!!!!!");
        }

    }
}

Any help would be appreciated. Thanks.


Solution

  • Issue was caused due to using infinite loop in service. To fix this issue instead of using infinite loop start the service with a thread and run for every 60 seconds.