7

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. 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..

"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."

Plz help me... Thanx in advance

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user2499974
  • 369
  • 3
  • 6
  • 11
  • 3
    XY problem. Remove the big load on OnStart() event. Thread off the crap. – Martin James Aug 22 '13 at 12:15
  • I cannot find enough information in your question to help you. You may consider debugging the problem, there are too many possible answers. – SQLMason Aug 22 '13 at 12:16
  • sir...actually , when i debug my service in visual studio 2010 then it work fine...but when i create exe file and install it on my system and goto task manager to start my service then timeout exception occur...this is my problem...now i googled the problem and found that , i will need to increase my startup time, which is by default 30sec... – user2499974 Aug 22 '13 at 12:34

5 Answers5

11

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.

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

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. If the pending operation is not a continue, pause, start, or stop, an InvalidOperationException is thrown.

Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • so i have to add ServiceBase.RequestAdditionalTime(4000); in Onstart(), OnPause(), OnStop(), OnContinue() events???? – user2499974 Aug 22 '13 at 12:40
  • Whereever you need to increase the default timeout. In your case you need it on start. But again not a good practice – Ehsan Aug 22 '13 at 12:42
  • @user2499974: Is there a reason you don't want to start a new thread? That is the correct solution. – Jay Sullivan Jan 07 '14 at 19:33
4

You better do your long operations in a Thread.

protected override void OnStart(string[] args)
{
  Thread thWorker = new Thread(new ThreadStart(
    delegate
    {
       // Do your long operations here
    }
  ));
  thWorker.Start();
}
Farzan
  • 745
  • 10
  • 25
  • You have to be careful here because if the delegate throws an exception, it will be swallowed instead of stopping/crashing the service, which you may actually want so that you know the service is not working, instead of it "running", but not actually doing anything or working properly. – deadlydog Jan 06 '17 at 22:26
  • @deadlydog by default, unhandled exceptions thrown in side threads do kill the process. See for example this discussion: https://stackoverflow.com/a/17203553/717732 – quetzalcoatl Dec 05 '19 at 14:17
2

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.

Alex Paven
  • 5,539
  • 2
  • 21
  • 35
1

Have you considered using task paraller library for this. This example is VB.Net but you get the idea:

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
Esko
  • 4,109
  • 2
  • 22
  • 37
0

to debug the OnStart of service (it can be a "long running task"), i use this:

    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