add wait thread

This commit is contained in:
Nguyen Dinh Thuan
2024-09-16 16:27:11 +07:00
parent 2b4a509e3a
commit 3e897ccee7
2 changed files with 43 additions and 2 deletions
+42 -2
View File
@@ -10,8 +10,10 @@ using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
@@ -40,6 +42,7 @@ namespace SafeBagDE70
{
try
{
WaitSQLStart();
CDMSTATE = new CDMSTATE();
DeviceStatus = new DeviceStatus
{
@@ -53,8 +56,6 @@ namespace SafeBagDE70
OtherError = 0,
};
InitUsers();
}
catch (Exception ex)
{
@@ -72,6 +73,7 @@ namespace SafeBagDE70
{
try
{
customerAccounts = new List<CustomerAccount>();
using (var context = new CDMDbContext())
{
@@ -101,6 +103,44 @@ namespace SafeBagDE70
}
}
private static void WaitSQLStart()
{
string serviceName = "MSSQLSERVER"; // Default SQL Server service name, change if necessary
// Create a ServiceController instance for the SQL Server service
ServiceController serviceController = new ServiceController(serviceName);
try
{
Log.Information("Checking the status of the SQL Server service...");
// Loop to wait until the service is running
while (serviceController.Status != ServiceControllerStatus.Running)
{
//if (serviceController.Status == ServiceControllerStatus.Stopped)
//{
// Log.Information("SQL Server service is stopped. Starting it...");
// serviceController.Start();
//}
// Refresh the controller to get the updated status
serviceController.Refresh();
Log.Information($"Current status: {serviceController.Status}");
Thread.Sleep(5000); // Wait for 5 seconds before checking again
}
Log.Information("SQL Server service is running successfully.");
}
catch (InvalidOperationException ex)
{
Console.WriteLine($"Error: Could not access the service '{serviceName}'. {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
public static CDMSTATE? CDMSTATE = null;
private static DateTime CutOffTime = new DateTime(1, 1, 1, 12, 0, 0); // 12:00 noon as cut-off time