Windows services 如何在停止topshelf托管的windows服务之前添加超时

Windows services 如何在停止topshelf托管的windows服务之前添加超时,windows-services,topshelf,Windows Services,Topshelf,我有许多windows服务,我已经安装使用顶架 我想知道是否有一种方法可以在停止服务时设置超时,以使服务当前正在处理的进程能够在服务实际关闭之前完成? 我想等待X秒,然后再终止顶部搁板 这是我作为服务安装的控制台: static int Main(string[] args) { return (int)HostFactory.Run(x => { string serviceName = new ServiceNam

我有许多windows服务,我已经安装使用顶架

我想知道是否有一种方法可以在停止服务时设置超时,以使服务当前正在处理的进程能够在服务实际关闭之前完成? 我想等待X秒,然后再终止顶部搁板

这是我作为服务安装的控制台:

    static int Main(string[] args)
    {
        return (int)HostFactory.Run(x =>
        {
            string serviceName = new ServiceNameResolverImpl().GetServiceName();
            x.SetServiceName(serviceName);
            x.SetDisplayName(serviceName);
            x.SetDescription(serviceName);

            x.UseAssemblyInfoForServiceInfo();
            x.RunAsLocalSystem();
            x.StartAutomaticallyDelayed();
            x.SetStartTimeout(TimeSpan.FromMinutes(1));
            x.Service<ServiceImpl>();
        });
    }
static int Main(字符串[]args)
{
返回(int)主机工厂。运行(x=>
{
字符串serviceName=newservicenameresolverimpl().GetServiceName();
x、 SetServiceName(服务名称);
x、 SetDisplayName(serviceName);
x、 SetDescription(serviceName);
x、 使用AssemblyInfoForServiceInfo();
x、 RunAsLocalSystem();
x、 start自动显示();
x、 设置开始超时(TimeSpan.FromMinutes(1));
x、 服务();
});
}

谢谢

在您的服务实现中,使用HostControl请求额外的关闭时间(我假定您的服务已经实现了
服务控制
,因为这是
服务
所期望的)-请注意,服务控制管理器可能会也可能不会批准该请求:

public class ServiceImpl : ServiceControl
{
     public bool Start(HostControl hostControl)
     {
          return true;
     }

     public bool Stop(HostControl hostControl) 
     {
          hostControl.RequestAdditionalTime(TimeSpan.FromMinutes(1));
          return true;
     }
}

与启动超时类似

x.SetStopTimeout(TimeSpan.FromMinutes(1));