Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Windows Service inside a dll
Message
De
04/09/2013 15:41:26
 
 
À
04/09/2013 12:12:12
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:
01581841
Message ID:
01582186
Vues:
43
>But what I couldn't understand is why you didn't have one class that *acted* like ServiceBase, but wasn't inherited from ServiceBase. It would have virtual/shared methods (like the ones you needed to use and override in your sub-classes). Then, your sub-classes, instead of inheriting from ServiceBase, would simply be inheriting from your "mock" ServiceBase class. It seems to me that that would have worked just the way you needed it to with minimal changes to your existing sub-classes (just change what they inherit from and the rest would be exactly the same).

Ok, lets revisit this one more time.

Here is the entry point:
Imports System.ServiceProcess

Public Class Main

    Shared oWindowsService As WindowsService = Nothing

    Shared Sub Main()

        ' If this from the Windows Service
        If Not Environment.UserInteractive Then
            Dim loWindowsService As WindowsService = New WindowsService()
            System.ServiceProcess.ServiceBase.Run(loWindowsService)
        Else
            oWindowsService = New WindowsService()
            oWindowsService.Start()
            Console.ReadLine()
            oWindowsService.Stop2()
        End If

    End Sub

End Class

Public Class WindowsService
    Inherits ServiceBase

    Public oRobot As Framework.Robot = New Framework.Robot
    Public oTimer As New System.Timers.Timer(5000)

    Public Sub New()
        CanStop = True
        CanShutdown = True
        CanPauseAndContinue = False
    End Sub

    Protected Overrides Sub OnShutdown()
        ' TODO: There may be a bug in the .NET Framework in that this method
        ' is not being called on system shutdown for SYSTEM account services.
        ' Not sure when/if it will be fixed, but I'm putting code here to stop
        ' stuff on the off-chance that it will be called eventually.

        ' stop code  here
    End Sub

    Protected Overrides Sub OnStart(ByVal toArgs As String())
        oRobot.oWindowsService = Me
        oRobot.Start()

        ' 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 Sub Start()
        OnStart(Nothing)
    End Sub

    ' 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)
        oRobot.ServiceTimer()
    End Sub

    Public Function Initialize() As Boolean
        oRobot.cDatabase = "MyDatabase"
        oRobot.lCloseApplicationOnMaximumLoop = True
        oApp.cMemberTable = "Member"
        oApp.cMemberTableFirstName = "Name"
        oApp.cMemberTableLastAccess = "LastHit"
        oApp.cMemberTableLastName = "Name"
        oApp.cTitle = "MyTitle"
        oApp.lEncryption = True
        oRobot.AddProcessFile("h:\Software", "RoboCopy.bat")
        Return True
    End Function
So, the first class is only to be able to instantiate the WindowsService class which inherits from ServiceBase. That one has to inherit from ServiceBase otherwise I would have inherit it from my framework class. So, I really cannot see how the oRobot can inherit automatically from Framework.Robot because it has to be defined in the class which is already inheriting from ServiceBase.

Do you see something I missed?
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