我正在编写一个依赖于其他服务的Windows服务,我应该如何等待其他服务启动?
谢谢
谢谢
我认为你应该这样写:
installer.ServicesDependedOn = new string[] { "依赖服务" };
就像这样:
using (ServiceProcessInstaller processInstaller = new ServiceProcessInstaller())
{
processInstaller.Account = ServiceAccount.LocalSystem;
processInstaller.Username = null;
processInstaller.Password = null;
using (ServiceInstaller installer = new ServiceInstaller())
{
installer.DisplayName = "yourservice.";
installer.StartType = ServiceStartMode.Automatic;
installer.ServiceName = "YourService";
installer.ServicesDependedOn = new string [] { "DependenceService" };
this.Installers.Add(processInstaller);
this.Installers.Add(installer);
}
}
祝你好运
public class DbStatus
{
public static bool DbOnline()
{
const int MaxRetries = 10;
int count = 0;
while (count < MaxRetries)
{
try
{
// Just access the database. any cheap query is ok since we don't care about the result.
return true;
}
catch (Exception ex)
{
Thread.Sleep(30000);
count++;
}
}
return false;
}
}
您需要指定依赖项。您可以在您的Installer类中完成此操作。
进一步澄清
您应该将 Installer 类作为自定义操作使用到您的设置项目中,以安装您的服务。如果没有,请发表评论,我会更新这个答案并告诉您如何操作。
在您的 Installer 类设计器中,您应该会看到两个组件:serviceInstaller 和 serviceProcessInstaller。我不记得其中哪一个了,但其中一个有一个属性,允许您指定一个多行字符串,列出您的服务所依赖的服务名称。
你对其他服务有控制权吗? 如果是的话,请让它们启动你,如果不是的话,我猜你还是得自己启动并监控正在发生的事情。 可以通过注册到WMI来在其他进程启动时收到通知 - 这方面有一个问题。