Starting a windows service

Asked

Viewed 837 times

0

I am mounting a "Service Monitor". My windows server is stopping my database service. So I set up a service validator, but I don’t know how to give the start in the service again in case he falls.

That’s the code of how far I’ve come catch, do the start in the 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)
            {

            }

1 answer

1


I need to make two considerations:

  1. 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 a crash in this your test and start restarting your database often for no reason.

    • Invalid username or password;
    • Maximum number of simultaneous connections achieved;
    • Timeout; etc.
  2. This script is just trying to make up the problem, and it’s not solving the cause.

Continue researching the cause of the problem, see on Eventviewer or in Mysql logs. But don’t leave to do a stunt like this.

About your question, to control a Windows Service Voce must use the class System.ServiceProcess.Servicecontroller.

Start 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
  {
    // ...
  }
}

Restart 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.