Starting a windows service

0

I'm setting up a "Service Monitor". My windows server is stopping the service from my database. So I mounted a service validator, but I do not know how to give the start service a new one if it crashes.

This is the code of how far I have come, the idea is when to drop catch , do the start in MySql service.

      while (true)
        {
            int contador = 0;

            try
            {
                MySqlConnection conn = new MySqlConnection(connstr);
                conn.Open();
                contador++;

                if (contador == 1)
                {
                    Console.Write(".");
                    contador = 0;
                }

                conn.Close();

                Thread.Sleep(30000);
            }
            catch (Exception ex)
            {

            }
    
asked by anonymous 15.07.2016 / 04:54

1 answer

1

I need to make two considerations:

  • Trying to connect to the bank to test whether or not it is working is not the best approach. There are many factors that can cause this test to fail and start restarting your database often for no reason.

    • Invalid username or password;
    • Maximum number of concurrent connections reached;
    • Timeout; etc.
  • This script is just trying to make up the problem, and is not solving the cause.

  • Continue searching for the cause of the problem, see EventViewer or the MySQL logs. But do not leave to make a mistake.

    On your question, to control a Windows Service you should use the System.ServiceProcess.ServiceController .

    Starting a service:

    public static void StartService(string serviceName, int timeoutMilliseconds)
    {
      var service = new ServiceController(serviceName);
      try
      {
        var timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
    
        service.Start();
        service.WaitForStatus(ServiceControllerStatus.Running, timeout);
      }
      catch
      {
        // ...
      }
    }
    

    Stop a service:

    public static void StopService(string serviceName, int timeoutMilliseconds)
    {
      var service = new ServiceController(serviceName);
      try
      {
        var timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
    
        service.Stop();
        service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
      }
      catch
      {
        // ...
      }
    }
    

    Restarting a service:

    public static void RestartService(string serviceName, int timeoutMilliseconds)
    {
      var service = new ServiceController(serviceName);
      try
      {
        var millisec1 = Environment.TickCount;
        var timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
    
        service.Stop();
        service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
    
        // count the rest of the timeout
        var millisec2 = Environment.TickCount;
        timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));
    
        service.Start();
        service.WaitForStatus(ServiceControllerStatus.Running, timeout);
      }
      catch
      {
        // ...
      }
    }
    

    Source: C # Examples

        
    15.07.2016 / 10:19