我有一個windows服務程序,它運行並有時會拋出有關係統空引用的異常。經過我的調查,這是由於MySQL連接無法建立,由於MySql實例在啓動計算機時尚未啓動。如何解決這個問題?如何在我的Windows服務之前啓動MYSQL服務
在此先感謝
我有一個windows服務程序,它運行並有時會拋出有關係統空引用的異常。經過我的調查,這是由於MySQL連接無法建立,由於MySql實例在啓動計算機時尚未啓動。如何解決這個問題?如何在我的Windows服務之前啓動MYSQL服務
在此先感謝
您可以在Windows的每個服務的相關設置。
您可以進入控制面板 - >管理工具 - >服務(或從命令行運行'services.msc')。通過雙擊任何服務並轉到依賴關係選項卡,您可以看到每個服務依賴的內容。
你的服務依賴於MySql服務,所以MySql應該在它的依賴關係列表中。
您可以將項目添加到依賴關係,這裏是如何做到這一點的描述:
,如果你想自己不依賴做到這一點試試下面的代碼
//Check if service is running
ServiceController mysqlServiceController = new ServiceController();
mysqlServiceController.ServiceName = "MySql";
var timeout = 3000;
//Check if the service is started
if (mysqlServiceController.Status == System.ServiceProcess.ServiceControllerStatus.Stopped
|| mysqlServiceController.Status == System.ServiceProcess.ServiceControllerStatus.Paused)
{
mysqlServiceController.Start();
try
{
//Wait till the service runs mysql
ServiceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running, new TimeSpan(0, timeout, 0));
}
catch (System.ServiceProcess.TimeoutException)
{
//MessageBox.Show(string.Format("Starting the service \"{0}\" has reached to a timeout of ({1}) minutes, please check the service.", mysqlServiceController.ServiceName, timeout));
}
}
+1不錯的鏈接文章,直接去泳池室 –
我會參考這個鏈接它的作品,但是當我將重新啓動計算機2次然後它的作品和一個空白的消息框顯示在電腦中味精框兩個按鈕顯示是或否,如果pres是的,它會重新啓動計算機&按否,它不會做任何事情,爲什麼這個按鈕顯示電腦 – user847455