Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
OnShutdown event in a Windows Service
Message
De
30/03/2014 13:25:08
 
 
À
30/03/2014 12:45:10
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01597504
Message ID:
01597702
Vues:
29
>I did a quick test on OnStop:
>RequestAdditionalTime(120000) then sleep for 100 secs : Service stopped after the 100 seconds.
>RequestAdditionalTime(300000) then sleep for 240 secs : SCM error "Service did not respond in timely fashion"
>
>Don't see any mention of maximum value but there's obviously a difference. FWIW WaitToKillServiceTimeout was 300000 but I don't think that should be a factor in OnStop ?

Well, this indicates there are some inconsistencies in there. 120000 should have 120 seconds and 300000 should have been 300 seconds. Also, the second one also created a situation which I believe is the same that I got. IAC, this approach is a no go. After more analysis, as you may have seen in other messages, the goal is to go higher at the pre shutdown event, which will force Windows to wait for any code to complete in there before initiating the shutdown event.

I have plugged it ok in the OnStart():
    Protected Overrides Sub OnStart(ByVal toArgs As String())
        oRobot.oWindowsService = Me

        ' If we cannot start
        If Not oRobot.Start() Then
            lStart = False

            ' If this from the Console application
            If Environment.UserInteractive Then
                Exit Sub
            End If

        End If

        Dim acceptedCommandsFieldInfo As System.Reflection.FieldInfo = GetType(ServiceBase).GetField("acceptedCommands", _
         System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic)
        If acceptedCommandsFieldInfo Is Nothing Then
            'Throw ApplicationException("acceptedCommands field not found")
        End If
        Dim value As Integer = CInt(acceptedCommandsFieldInfo.GetValue(Me))
        acceptedCommandsFieldInfo.SetValue(Me, value Or SERVICE_ACCEPT_PRESHUTDOWN)

        ' Define the timer
        AddHandler oTimer.Elapsed, AddressOf ServiceTimer

        ' Start the timer
        oTimer.Enabled = True

    End Sub
But, on shutdown, the event is never fired:
    Protected Overrides Sub OnCustomCommand(command As Integer)

        If command = SERVICE_CONTROL_PRESHUTDOWN Then

            ' If the robot is busy
            If oRobot.lBusy Then
                oRobot.lShutDown = True
                RequestAdditionalTime(30000)
            Else

                ' If we cannot stop the robot
                If Not oRobot.StopRobot() Then

                End If

            End If

        Else
            MyBase.OnCustomCommand(command)
        End If

    End Sub
I am still looking at this to see why the event is not fired. But, I am assuming this might be the way to do it, for our context, to control the general Windows shutdown. So, clean code will be executed to complete everything, all resources will be available because the shutdown event would not be fired yet, then once ok, the services will be terminated.
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform