简体   繁体   English

需要增加窗口服务超时

[英]Need to increase window service timeout

i am facing problem during starting my window service... as there is a big load on OnStart() event of my service, it scrap data, saved it to database and send email. 我在启动窗口服务时遇到问题...因为我的服务的OnStart()事件有很大的负担,它会废弃数据,将其保存到数据库并发送电子邮件。 So my service need to increase start time because the defualt timeout is 30second... i have released that my service will need additional time to start when i face the following exception.. 所以我的服务需要增加开始时间,因为defualt超时是30秒......我已经发布了当我遇到以下异常时我的服务将需要额外的时间来启动..

"Could not start the MyName service on Local Computer. Error 1053: The service did not respond to the start or control request in a timely fashion." “无法在本地计算机上启动MyName服务。错误1053:服务未及时响应启动或控制请求。”

Plz help me... Thanx in advance Plz帮我... Thanx提前

i have realised that my service will need additional time to start when i face the following exception 我意识到当我遇到以下异常时,我的服务需要额外的时间才能开始

doing long runnings tasks on constructor/start isn't good . 在构造函数/启动上执行长运行任务并不好 you should start your long running task on a sperate thread. 你应该在sperate线程上开始你的长期运行任务。

Service startup should be instant and should not hang up. 服务启动应该是即时的,不应该挂断。

However if you still want, you can do this 但是,如果你仍然想要,你可以这样做

ServiceBase.RequestAdditionalTime(4000); // add 4 seconds

From MSDN 来自MSDN

The RequestAdditionalTime method is intended to be called by the overridden OnContinue, OnPause, OnStart, or OnStop methods to request additional time for a pending operation, to prevent the Service Control Manager (SCM) from marking the service as not responding. RequestAdditionalTime方法旨在由重写的OnContinue,OnPause,OnStart或OnStop方法调用,以请求待处理操作的额外时间,以防止服务控制管理器(SCM)将服务标记为未响应。 If the pending operation is not a continue, pause, start, or stop, an InvalidOperationException is thrown. 如果挂起的操作不是continue,pause,start或stop,则抛出InvalidOperationException。

You better do your long operations in a Thread. 你最好在Thread中进行长时间的操作。

protected override void OnStart(string[] args)
{
  Thread thWorker = new Thread(new ThreadStart(
    delegate
    {
       // Do your long operations here
    }
  ));
  thWorker.Start();
}

As far as I know that hard limit is there exactly to prevent this sort of abusive behavior from services :) 据我所知,硬限制正是为了防止服务中的这种滥用行为:)

Make your long running tasks run outside the startup of the service. 使长时间运行的任务在服务启动之外运行。 Handle stopping the service gracefully, then you can automatically stop the service when it's done if you need to. 处理优雅地停止服务,然后您可以在需要时自动停止服务。 There's no need to do everything on startup. 启动时无需做任何事情。

to debug the OnStart of service (it can be a "long running task"), i use this: 调试OnStart服务(它可以是一个“长时间运行的任务”),我用这个:

    Protected Overrides Sub OnStart(ByVal args() As String)
 #If CONFIG = "Debug" Then
        ' 2 minutes before timeout
        Me.RequestAdditionalTime(2 * 60 * 1000)
        Debugger.Launch()
 #End If
.
.
.
    End Sub

Have you considered using task paraller library for this. 你考虑过使用task paraller库吗 This example is VB.Net but you get the idea: 这个例子是VB.Net,但你明白了:

Imports System.Threading.Tasks

Public Class Service1

    Private tasks As New List(Of Task)

    Protected Overrides Sub OnStart(ByVal args() As String)
        tasks.Add(Task.Factory.StartNew(Sub() DoWork()))
    End Sub

    Private Sub DoWork()
        ' Do long running work
    End Sub

    Protected Overrides Sub OnStop()
        Task.WaitAll(tasks.ToArray())
    End Sub

End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM