Rust Cohle February 2016

Windows Service installed. But start, stop buttons disabled

I created windows service. Installed with Installutil.exe success, but when i press start button appears progress bar "starting", then it stopped at 50 percent. I have to cancel. After cancel service works perfect, but start, stop and pasue commands disabled. If i want to stop service, i have to uninstall service. What can be reason ?

Main method:

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

It is my service class:

partial class TelegramBotService : ServiceBase
{
    private static Logger logger = LogManager.GetCurrentClassLogger();
    public TelegramBotService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        try
        {
            RunBot().Wait();
            logger.Info("Start windows service");
        }
        catch(Exception ex)
        {
            logger.Error(ex.Message);
        }
    }

    private async Task RunBot()
    {
        try
        {

          logger.Info("Start Telegram Bot");
          //var ElectionsBot = new ElectionsInfo();
          var ElectionsBot = new ElectionsCount();
          await ElectionsBot.Start();
        }
        catch(Exception ex)
        {
            logger.Error(ex.Message);
        }
    }

    protected override void OnStop()
    {
        logger.Info("Stop windows service");
    }
}

Answers


RoadWarrior February 2016

The OnStart method must finish within 10 seconds, otherwise the Service Control Manager thinks that it's hung. Currently it's waiting indefinitely for the RunBot task to finish.

I've described a good way to deal with this approach. This is my OnStart method, which launches the service's controller thread:

// SCM requests service start using its own thread.
// This method must complete within 10 seconds of it
// starting. Otherwise the SCM diagnoses a hang.
protected override void OnStart(string[] args)
{
    AppDomain currentDomain = AppDomain.CurrentDomain;
    currentDomain.UnhandledException += new UnhandledExceptionEventHandler(this.UnhandledExceptionFilter);

    this.ThreadController = new Thread(new ThreadStart(ControllerThreadRunning));
    this.ThreadController.Name = THREAD_NAME_CONTROLLER;
    this.ThreadController.Start();
    base.OnStart(args);
}

This is the controller thread:

// Invoked when the controller thread starts. 
private void ControllerThreadRunning()
{
    // And we're on our way.
    while ( !this.ServiceStopRequested )
    {
        // Start real work and then block until that finishes or crashes.
        var realWork = this.LaunchWorkAsync();
        realWork.Wait();
        // If SCM didn't request a service stop, the assumption is that 
        // the real work crashed and needs to be restarted.
        if ( !this.ServiceStopRequested )
        {
            this.PauseControllerThread("Pause before restarting work cycle.", this.RestartDelay);
        }
    }

    // This service will now stop.
    this.Cleanup();
}

This asynchronously launches the service's real work:

// This method handles all ceremony around the real work of this service.
private async Task LaunchWorkAsync()
{
    try 
    {
        // Setup progress reporting.
    

Post Status

Asked in February 2016
Viewed 3,744 times
Voted 12
Answered 1 times

Search




Leave an answer