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:
Imports System.IO
Imports System.Timers
Public Class OutlookService
Protected Overrides Sub OnStart(ByVal args() As String)
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