Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Stopping a service from the OnStart() method
Message
De
04/09/2013 23:55:42
 
 
À
04/09/2013 22:04:06
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:
01582238
Message ID:
01582251
Vues:
34
It seems a Windows Service is not meant to be stopped from the inside. Those methods are called when someone stops the service from the Services interface, from another application, from sc or else.

When the Windows Service is run as a Console mode, this is not a problem. See the code below. However, when the Windows Service is run as a service, we cannot stop it from the OnStart() method. Someone mentioned to throw an error in there. That will stop the service but will also throw a message box on the console. So, I needed to find a way to stop the service from the inside with no message on the console. The trick was to set up a flag in the OnStart() method, lower the timer to one second, so we won't waste any time waiting for it to close, and in the ServiceTimer() event, verify that flag and call the ServiceController to stop it.
    Public oRobot As Framework.Robot = New Framework.Robot
    Public oTimer As New System.Timers.Timer(5000)
    Private lStart As Boolean = False

    Protected Overrides Sub OnStart(ByVal toArgs As String())

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

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

                ' Put 1 second so to have the timer to fire up as soon as the last line in here will be executed
                ' This is to have the ServiceTimer() event to fire immediately as it is not possible to stop it from here
                oTimer.Interval = 1000

            End If

        End If

        ' Define the timer
        AddHandler oTimer.Elapsed, AddressOf ServiceTimer

        ' Start the timer
        oTimer.Enabled = True

    End Sub

    Protected Overrides Sub OnStop()
    End Sub

    ' This method is only used when not runnning as a Service
    Public Function Start() As Boolean

        ' Initialization
        lStart = True

        OnStart(Nothing)

        ' If we were not able to start
        If Not lStart Then
            Return False
        End If

        Return True
    End Function

    ' This method is only used when not runnning as a Service
    Public Sub Stop2()
        OnStop()
    End Sub

    Private Sub ServiceTimer(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs)

        ' If we did not want the service to start at first
        If Not lStart Then

            ' If this from the Windows Service
            If Not Environment.UserInteractive Then
                Dim loServiceController As ServiceController = New ServiceController("YourServiceNameHere")
                loServiceController.Stop()
            End If

        End If

        oRobot.ServiceTimer()
    End Sub
If someone has a better way to do it, feel free to provide feedback.

Note that this approach works as a console mode and as a Windows Service mode.
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
Répondre
Fil
Voir

Click here to load this message in the networking platform