From 3e897ccee7b65f5e92c706529e8e320d533786ff Mon Sep 17 00:00:00 2001 From: Nguyen Dinh Thuan Date: Mon, 16 Sep 2024 16:27:11 +0700 Subject: [PATCH] add wait thread --- AppData.cs | 44 ++++++++++++++++++++++++++++++++++++++++++-- SafeBagDE70.csproj | 1 + 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/AppData.cs b/AppData.cs index 2dd4aaa..dc52016 100644 --- a/AppData.cs +++ b/AppData.cs @@ -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(); 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 diff --git a/SafeBagDE70.csproj b/SafeBagDE70.csproj index b0ea94c..849de47 100644 --- a/SafeBagDE70.csproj +++ b/SafeBagDE70.csproj @@ -69,6 +69,7 @@ +