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_PAUSED = &H7
    End Enum

    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()
    End Sub

    Public Sub OnDebug()
    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)


        thetvdb = New clsTheTVDBv2()

        Dim timr As Timer = New Timer
        'timr.Interval = 60000
        timr.Interval = TimeSpan.FromHours(24).TotalMilliseconds
        AddHandler timr.Elapsed, AddressOf Me.OnTimer
        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()
            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)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Public Sub OnTimer(sender As Object, args As ElapsedEventArgs)

            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")
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class

Recommended Answers

All 4 Replies

I suggest you use the Windows Task Scheduler to run your program at the given interval rather than relying on your program to continue running in the background. Using WTS would cover you if your program were to be interrupted for any reason (reboots, etc.). Plus it simplifies your code.

I did consider using WTS. And although that provides all that you mentioned, it still didn't appeal to me.
Thank you for your reply. Truly. But not what I was asking for.

I'm wondering why you set the service status explicitly? If you have created a windows service app from Visual Studio, those should be set automatically.

While a Timer is great for testing. If you want something more robust and configurable, have a look at : https://www.quartz-scheduler.net/

I don't know if you made any mistakes, but my advice would be to log the exceptions to the event log so that it's known when it doesn't successfully run and why.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.