[英]how to start the MYSQL service before my Windows service
我有 windows 服务程序,它运行并且有时抛出关于系统 null 参考的异常。 经过我的调查,这是由于 MySQL 连接无法建立,因为 MySql 实例在启动计算机时尚未启动。 这个问题怎么解决???
提前致谢
您可以在 Windows 中设置每个服务的依赖关系。
您可以 go 进入控制面板 -> 管理工具 -> 服务(或从命令行运行“services.msc”)。 通过双击任何服务并转到 Dependencies 选项卡,您可以查看每个服务所依赖的内容。
您的服务依赖于 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));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.