Я пишу приложение, чтобы автоматически перезапустить службу SQL. В основном, это те шаги, которые я делаю:SQL служба не запускается должным образом с помощью ServiceController
- Закрыть приложение службы
- Остановка службы SQL
- Запуск SQL
- Запуск приложения
Проблема у меня есть с запуском приложения , Похоже, что приложение запускается до службы SQL, тем самым вызывая ошибку подключения к базе данных. Я делаю проверку на статус службы по следующим направлениям, но она по-прежнему не работает:
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
if (service.Status == ServiceControllerStatus.Stopped)
Код:
static void Main(string[] args)
{
PrintMessage("arg 0: " + args[0]);
PrintMessage("arg 1: " + args[1]);
PrintMessage("arg 2: " + args[2]);
Console.Title = "SQL Server Restart";
PrintMessage("Commencing SQL Server restart. Please wait. . .");
string serverInstance = args[0];
ServiceController service = new ServiceController(serverInstance);
try
{
if (service.Status == ServiceControllerStatus.Running)
{
PrintMessage("Checking server status");
TimeSpan timeout = TimeSpan.FromMinutes(5);
PrintMessage("Stoppping server");
service.Stop();
service.Refresh();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
if (service.Status == ServiceControllerStatus.Stopped)
{
PrintMessage("Starting server");
service.Start();
service.Refresh();
service.WaitForStatus(ServiceControllerStatus.Running);
if (service.Status == ServiceControllerStatus.Running)
{
PrintMessage("Server started");
//Restart application
if (args[2] == "False")
{
PrintMessage("Press any key to continue. . .");
Console.ReadKey();
Environment.Exit(0);
}
else
{
PrintMessage("Starting Application");
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = args[1];
info.WorkingDirectory = Path.GetDirectoryName(info.FileName);
Process process = new Process();
process.StartInfo.UseShellExecute = false;
Process.Start(info);
PrintMessage("Application started");
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Base Exception: " + ex.GetBaseException());
Console.WriteLine("Inner Exception: " + ex.InnerException);
Console.WriteLine(ex.Message);
}
PrintMessage("Press any key to continue. . .");
Console.ReadKey();
}
Какова Ваша цель? Его приложение? –