1
votes

I made a windows service using C# as follow

public partial class Housekeeping : ServiceBase
{
    #region Fields
    private ManualResetEvent _ResetEvent = new ManualResetEvent(false);
    private RegisteredWaitHandle _RegisteredWaitHandle;
    private long _Interval = 60000;
    private Logger _Logger;
    #endregion

    #region Constructors
    public Housekeeping()
    {
        InitializeComponent();
        _Interval = _Interval * Convert.ToInt32(ConfigurationManager.AppSettings["RunningInterval"]);
        _Logger = new Logger();
    }
    #endregion

    #region Properties

    #endregion

    #region Behaviors
    public void Housekeep(object state, bool timeout)
    {
        try
        {
            // my code
        }
        catch (Exception ex)
        {
             // my code
        }
    }
    protected override void OnStart(string[] args)
    {
        _RegisteredWaitHandle = ThreadPool.RegisterWaitForSingleObject(_ResetEvent, new WaitOrTimerCallback(Housekeep), null, _Interval, false);
    }
    protected override void OnStop()
    {

    }
    protected override void OnContinue()
    {
        base.OnContinue();
    }
    protected override void OnPause()
    {
        base.OnPause();;
    }
}

and on the main

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[]
        {
            new Housekeeping()
        };
        ServiceBase.Run(ServicesToRun);
    }
}

I Installed the WS using InstallUtil.exe, but when I tried to start the WS I got this error "error 1053: the service did not respond to the start or control request in a timely fashion". I surfed around but all the solution are for a windows server 2003 issue and I'm running Windows 10.

How can I solve this issue?

1
The most likely cause is a problem in your Main() function. Please show the rest of the code.Harry Johnston
Harry Johnston, I updated to the code I'm using. Thanks.Victor A Chavez
I'm not very familiar with the .NET ServiceBase class, but this looks OK to me. The only thing I can suggest is that perhaps the constructor isn't returning for some reason - maybe put in some logging to see whether you're reaching ServiceBase.Run and/or whether OnStart() is actually being called.Harry Johnston

1 Answers

0
votes

Last time I have seen this issue, it got resolved by changing the compiling option from 'Debug' to 'Release'.