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:12

Check ConnectionStrings if you are using EntityFramework or any other means to initiate database connections at the service startup.

In my case when i got the error Error 1053 the service did not respond to the start or control request in a timely fashion this is what was going wrong:

I was using EntityFramework and had the connection strings wrong. So basically at startup the EntityFramework failed to connect to the database using the incorrect Connection String and timed out.

Just had to replace the database connection string with the correct one and it worked fine.

Did not need any framework update or any other system/configuration change at all.

查看更多
乱世女痞
3楼-- · 2020-05-18 05:13

I had same problem. Unchecking "Sing the ClickOnce manifest", "Sign the assembly" and "Enable ClickOnce security settings" in project properties helped

查看更多
混吃等死
4楼-- · 2020-05-18 05:15

In my experience, I had to stop my existing service to update code. after updating the code and while START the Service I got the same error "Error 1053 the service did not respond to the start or control request in a timely fashion".

But this got resolve after RESTARTING THE MACHINE.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2020-05-18 05:15

Check if the service starting code is correct,

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

Also, remove any debug codes. ie,

  #If Debug
         ...
         ...
         ...
        #else
         ...
         ...
        #endif
查看更多
SAY GOODBYE
6楼-- · 2020-05-18 05:16

I have just tried this code locally in .Net 4.5 and the service starts and stops correctly for me. I suspect your problem may be around creating the EventLog source.

The method:

EventLog.SourceExists("MySource")

requires that the user running the code must be an administrator, as per the documentation here:

http://msdn.microsoft.com/en-us/library/x7y6sy21(v=vs.110).aspx

Check that the service is running as a user that has administrator privileges.

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

Install the .net framework 4.5! It worked for me.

https://www.microsoft.com/en-us/download/details.aspx?id=57768

查看更多
登录 后发表回答