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