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 05:17

Also, you need to check your configuration file content.

You need to check below the section.

<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>

Coz above section need to match with yours .net framework.

查看更多
Fickle 薄情
3楼-- · 2020-05-18 05:18

It is because of the Microsoft Windows Service Control, it controls sometimes the state of the services. If the service don´t send a respond in 30 seconds, then you will have this error.

You can modified the registry, so the service will have more time to respond

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

Or be sure that in that moment there is not another process talking with the service, maybe there is a conflict I don´t know

查看更多
看我几分像从前
4楼-- · 2020-05-18 05:18

this error can be caused due to various reasons. to identify the reason add try/ catch when service is run.

        try
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new Service1() 
                };
                <span class="skimlinks-unlinked">ServiceBase.Run(ServicesToRun</span>);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("Application", ex.ToString(), <span class="skimlinks-unlinked">EventLogEntryType.Error</span>);
            }
查看更多
Emotional °昔
5楼-- · 2020-05-18 05:19

In the case I ran into this morning, the culprit was a malformed config file. The config file had an close comment tag without the open comment tag. So, double check your config files for errors.

查看更多
唯我独甜
6楼-- · 2020-05-18 05:20

In my case, I was publishing service while it was in debug mode.

Solution was:

  • Changing solution to release mode
  • Uninstall old service with command InstallUtil -u WindowsServiceName.exe
  • installing service again InstallUtil -i WindowsServiceName.exe

It worked perfectly after.

查看更多
再贱就再见
7楼-- · 2020-05-18 05:21

I was getting exactly same issue, All I have done is to to change the Debug mode to Release while compiling the dll. This has solved my probelm, how/why? I dont know I have already asked a question on SO

查看更多
登录 后发表回答