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 SubBut, 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 SubI 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.