Error 1053 the service did not respond to the star

2020-05-18 04:27发布

I have created and installed a service a couple of times. Initially it was working fine, but after some changes in the service Code it start giving the error when I restart the service in Services.msc :

Error 1053: the service did not respond to the start or control request in a timely fashion

Code:

public partial class AutoSMS : ServiceBase
{
    public AutoSMS()
    {
        InitializeComponent();
        eventLog1.Clear();

        if (!System.Diagnostics.EventLog.SourceExists("MySource"))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "MySource", "MyNewLog");
        }
        eventLog1.Source = "MySource";
        eventLog1.Log = "MyNewLog";

        Timer checkForTime = new Timer(5000);
        checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
        checkForTime.Enabled = true;

    }

    protected override void OnStart(string[] args)
    {
        eventLog1.WriteEntry("In OnStart");
    }

    protected override void OnStop()
    {
        eventLog1.WriteEntry("In onStop.");
    }


    void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
    {
        string Time = "15:05:00";
        DateTime dateTime = DateTime.ParseExact(Time, "HH:mm:ss",
                                        CultureInfo.InvariantCulture);

        if (DateTime.Now == dateTime) ;
            eventLog1.WriteEntry(Time);
    }
}

Here is my main method code

static void Main()
{
    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[] 
    { 
        new AutoSMS() 
    };
    ServiceBase.Run(ServicesToRun);
}

I also tried the following steps :

  • Go to Start > Run > and type regedit
  • Navigate to: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
  • With the control folder selected, right click in the pane on the right and - select new DWORD Value
  • Name the new DWORD: ServicesPipeTimeout
  • Right-click ServicesPipeTimeout, and then click Modify
  • Click Decimal, type '180000', and then click OK
  • Restart the computer

I used to install and uninstall it with following command :

installutil AutoSMS.exe

installutil /u AutoSMS.exe

标签: c# .net timer
30条回答
该账号已被封号
2楼-- · 2020-05-18 04:56

As others have pointed out, this error can have a multitude of causes. But in the hopes that this will help somebody out, I'll share what happened in our case. For us, our service had been upgraded to .NET 4.5, but the server did not have .NET 4.5 installed.

查看更多
Bombasti
3楼-- · 2020-05-18 05:00

I have removed

EventLog.Exists

and fixed.

查看更多
放我归山
4楼-- · 2020-05-18 05:01

I know this is old question, I used to write my own VB.NET windows service, and it has no issue to start on MS windows 7 and MS windows 10.

I have this issue when I install the windows services on latest MS windows 10 patch. The reason the windows service doesn't run it is because the .NET version that needed for the window services to run is not presented in the installed PC.

After you have installed the windows services. go to the install folder for example C:\Program files (x86)\Service1\Service1.exe and double click to run it. If there is missing .NET framework package, it will prompt the user to download it. Just download and and wait for it to install.

After that restart the windows services in services.msc. Hope this answer will help someone who face the issue. I know issue is caused by .NET framework version.

查看更多
祖国的老花朵
5楼-- · 2020-05-18 05:02

This is usually caused by an uncaught exception in the service itself. (Configuration file errors eg). Opening a command prompt and starting the service executable manually wil perhaps reveal the exception been thrown.

查看更多
ゆ 、 Hurt°
6楼-- · 2020-05-18 05:04

I scratched my head to clear this error This error might be caused if you are debugging it in the code like

static void Main()
{
 #if DEBUG
            MailService service = new MailService();
             service.Ondebug();
 #else
             ServiceBase[] ServicesToRun;
             ServicesToRun = new ServiceBase[]
             {
                 new MailService()
             };
             ServiceBase.Run(ServicesToRun);
 #endif
         }
     }

After clearing the if,else and endif in the code like this the error has not appeared again....hope it helps....

static void Main()
{

    ServiceBase[] ServicesToRun;
    ServicesToRun = new ServiceBase[]
    {
        new MailService()
    };
    ServiceBase.Run(ServicesToRun);

}
查看更多
狗以群分
7楼-- · 2020-05-18 05:05

After spending some time on the issue, trying solutions that didn't work, I run into this blog. It suggests to wrap the service initialization code in a try/catch block, like this, and adding EventLog

using System;
using System.Diagnostics;
using System.ServiceProcess;

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

Then, uninstall the old service, redeploy the service with these modifications. Start the service and check out the Event Viewer/Application logs. You'll see what the real problem is, which is the underlying reason for the timeout.

查看更多
登录 后发表回答