Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I wrote s simple service that connects to a mysql server.
In the "OnStart" subrutine, the service try to connect to DB and start a timer.

my problem is that by stopping the PC the service does not stop and the "OnStart" subrutine is not executed the next time it is turned on. How can I solve this problem? I attach the code written in the service.

Thanks

Semola73

What I have tried:

VB
Imports System.IO
Imports System.Timers
Public Class OutlookService

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Inserire qui il codice necessario per avviare il proprio servizio. Il metodo deve effettuare
        ' le impostazioni necessarie per il funzionamento del servizio.
        AddHandler Microsoft.Win32.SystemEvents.SessionEnded, AddressOf OnSessionEnded

        Dim swc_log As Boolean
        Dim ws_ini As New iniParser("C:\UsrPrograms\OutlookService\OutlookService.ini")
        swc_log = Boolean.Parse(ws_ini.getValue("GENERALE", "SWC_LOG"))
        ws_log = New Log(swc_log)

        ws_server = ws_ini.getValue("GENERALE", "SERVER")
        ws_username = ws_ini.getValue("GENERALE", "USER_NAME")
        ws_password = ws_ini.getValue("GENERALE", "USER_PWD")
        ws_database = ws_ini.getValue("GENERALE", "DATABASE")

        ws_OSQL = New OutlookSQL()
        If ws_OSQL.ConnectDb(ws_server, ws_username, ws_password, ws_database) Then

            ws_iniDb = New IniParserDB
            PathDocuments = ws_iniDb.getParam("GENERALE", "PATH_DOCUMENTS").getValore
            PathDropbox = ws_iniDb.getParam("GENERALE", "PATH_DROPBOX").getValore
            TempDir = ws_iniDb.getParam("GENERALE", "TEMP_DIR").getValore

            Try
                Boolean.TryParse(ws_iniDb.getParam("GENERALE", "DEBUG").getValore, swc_debug)
            Catch ex As Exception
                swc_debug = False
            End Try

            ws_timer = New Timer()
            AddHandler ws_timer.Elapsed, AddressOf OnTimedEvent
            ws_timer.Interval = CDbl(ws_iniDb.getParam("GENERALE", "INTERVAL").getValore) * 1000
            ws_timer.Enabled = True
        End If

    End Sub
    Private Sub OnSessionEnded()
        ws_log.ScriviLog("Windows Shutdown - Stop Service")
        Me.Stop()
    End Sub

    Protected Overrides Sub OnShutdown()
        Me.Stop()
    End Sub

    Protected Overrides Sub OnStop()
        ws_timer.Enabled = False

        ws_OSQL.Dispose()
        ws_OSQL = Nothing

        ws_log.Flush()
        ws_log.Dispose()
        ws_log = Nothing
    End Sub

    Private Sub OnTimedEvent()

        ws_log.ScriviLog("Try to analize files in: " & TempDir)
        For Each myFilename In Directory.GetFiles(TempDir)
            ws_log.ScriviLog("  Found file to elaborate: " & myFilename)
            Dim OutEng As New OutlookEngine(myFilename)
        Next

    End Sub
Posted
Updated 14-Jun-20 20:09pm

1 solution

Im not entirely sure what you mean by 'service', but it seems from your code you have built a VB.Net Win32 Service - so when you view the Windows Services Panel or use 'SC' to list services a) yours is there and b) it's type is set to 'Auto Start' or similar .. yes ?

(if it's not set to Auto Start, and Im on a Mac atm so cannot check the exact value, use the Service Panel and modify the service definition to change it's start up type to start automatically, restart your PC and see what happens)

Else, other things you may need to do, is/are
a) check the Event log(s) for messages that may show why your service isnt starting properly
b) add logging to the OnStart so you can trace it and see where any difficulties might lie - debugging a service without logging is hard as I guess you know - Im used to using C# and 'TopShelf' so I can test the service inner workings as a command-line application before installing as a service
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900