Hi!
It's been a while since I last posted here.
I've just gotten started with Windows Service, and have created a sort of 24/7 service to update a database.
But because it's my very first project (besides the tutorial one) I feel that I could use some assistance in checking it over.
The intent is to have it run a method from a class at about once every 24 hours.
If anyone of you clever people could spare a moment to see if I've made mistakes.
(Ignore the database connections. Those work)
Imports System
Imports System.Timers
Imports System.Runtime.InteropServices
Public Class FileRenamerSrv
Private thetvdb As clsTheTVDBv2
<DllImport("advapi32.dll", SetLastError:=True)>
Private Shared Function SetServiceStatus(ByVal handle As System.IntPtr, ByRef serviceStatus As ServiceStatus) As Boolean
End Function
Public Enum ServiceState
SERVICE_STOPPED = &H1
SERVICE_START_PENDING = &H2
SERVICE_STOP_PENDING = &H3
SERVICE_RUNNING = &H4
SERVICE_CONTINUE_PENDING = &H5
SERVICE_PAUSE_PENDING = &H6
SERVICE_PAUSED = &H7
End Enum
<StructLayout(LayoutKind.Sequential)>
Public Structure ServiceStatus
Public dwServiceType As Integer
Public dwCurrentState As ServiceState
Public dwControlsAccepted As Integer
Public dwWin32ExitCode As Integer
Public dwServiceSpecificExitCode As Integer
Public dwCheckPoint As Integer
Public dwWaitHint As Integer
End Structure
Public Sub New()
InitializeComponent()
End Sub
Public Sub OnDebug()
OnStart(Nothing)
End Sub
Protected Overrides Sub OnStart(ByVal args() As String)
Dim serviceStatus1 As ServiceStatus = New ServiceStatus
serviceStatus1.dwCurrentState = ServiceState.SERVICE_START_PENDING
serviceStatus1.dwWaitHint = 100000
SetServiceStatus(Me.ServiceHandle, serviceStatus1)
InitializeDatabase()
PreLoadUpdate()
thetvdb = New clsTheTVDBv2()
Dim timr As Timer = New Timer
'timr.Interval = 60000
timr.Interval = TimeSpan.FromHours(24).TotalMilliseconds
AddHandler timr.Elapsed, AddressOf Me.OnTimer
timr.Start()
OnTimer(Nothing, Nothing)
serviceStatus1.dwCurrentState = ServiceState.SERVICE_RUNNING
SetServiceStatus(Me.ServiceHandle, serviceStatus1)
End Sub
Protected Overrides Sub OnStop()
Dim serviceStatus1 As ServiceStatus = New ServiceStatus
thetvdb = Nothing
serviceStatus1.dwCurrentState = ServiceState.SERVICE_STOPPED
SetServiceStatus(Me.ServiceHandle, serviceStatus1)
End Sub
Private Sub PreLoadUpdate()
Try
Dim rows() As DataRow = FileRenamerDataSet1.TitleTable.Select("RenameDate IS NULL")
If rows.Length > 0 Then
For Each row As DataRow In rows
row("RenameDate") = DateTime.Now.AddDays(-1)
Next
TitleTableTableAdapter1.Update(FileRenamerDataSet1.TitleTable)
TitleTableTableAdapter1.Fill(FileRenamerDataSet1.TitleTable)
End If
Catch ex As Exception
End Try
End Sub
Public Sub OnTimer(sender As Object, args As ElapsedEventArgs)
Try
thetvdb.UpdateDatabase()
Dim lastupdated As DateTime = DateTime.Parse(FileRenamerDataSet1.LastUpdated.Rows(0).Item(1).ToString)
If Not lastupdated.Date = Now.Date Then
Dim row As DataRow = FileRenamerDataSet1.LastUpdated.Rows(0)
row(1) = DateTime.Now.ToString("yyyy-MM-dd")
LastUpdatedTableAdapter1.Update(FileRenamerDataSet1.LastUpdated)
LastUpdatedTableAdapter1.Fill(FileRenamerDataSet1.LastUpdated)
End If
Catch ex As Exception
End Try
End Sub
End Class