troubleshooting Question

Windows Service Timer event not firing

Avatar of jayknight
jayknight asked on
Visual Basic.NET
14 Comments2 Solutions1297 ViewsLast Modified:
Below is my code for service1.vb. I have created a timer, but it's not firing the elapsed event. What am I doing wrong?

TIA!
P.s.: I removed the component designer generated code for this sample, let me know if you need it.
=============================================================================
Imports System.ServiceProcess
Imports System.IO

Public Class Service1
    Inherits System.ServiceProcess.ServiceBase

'Component Designer Generated Code

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
    End Sub
    Private Sub CreateTimer()
        Dim Timer1 As New System.Timers.Timer
        Timer1.Interval = 5000
        Timer1.Enabled = True
        AddHandler Timer1.Elapsed, _
           New System.Timers.ElapsedEventHandler(AddressOf Me.Timer1_Elapsed)
    End Sub

    Private Sub Timer1_Elapsed(ByVal sender As System.Object, _
       ByVal e As System.Timers.ElapsedEventArgs)
        'System.Windows.Forms.MessageBox.Show("Elapsed!", "Timer Event Raised!")
        Dim SourcePath As String = "Y:\SigFiles\"
        Dim DestPath As String = "C:\SigFiles\"
        Dim FileMove As String
        'Dim Directory As New System.IO.DirectoryInfo(SourcePath)
        Dim fi As System.IO.FileInfo
        For Each sFile As String In Directory.GetFiles(SourcePath)
            fi = New System.IO.FileInfo(sFile)
            FileMove = Right(fi.Name, 16)
            FileMove = Left(FileMove, 6)
            If Not System.IO.Directory.Exists(DestPath & FileMove) Then System.IO.Directory.CreateDirectory(DestPath & FileMove)
            'move files to subdirectory based on name
            System.IO.File.Move(sFile, DestPath & FileMove & "\" & fi.Name)
        Next
    End Sub
End Class
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 14 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 14 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros