我有一个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