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 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.
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