How to make a windows service run automatically at a particular time interval?

ipjyo
ipjyo used Ask the Experts™
on
Hi,
I have a windows service which runs manually and looks for a file in a directory using FileSystemWatcher. Once the service started, whenever there is a new file it is getting processed.
Now I need to modify this to run automatically every 60 seconds. Where should I include the timer? Any idea is helpful. I have attached my current code.
Thank you
Private Sub IntializeFileSystemWatcher()
            'Create File System Watcher for DAT files
            fsWatcher = New System.IO.FileSystemWatcher("C:\Sample Files", "*.DAT")
            ' Add event handlers for new files and change of existing files.
            'fsWatcher.Changed += new FileSystemEventHandler(OnFileChanged)
            AddHandler fsWatcher.Created, AddressOf OnFileCreated
            ' Begin watching.
            fsWatcher.EnableRaisingEvents = True
        End Sub
       
 Private Sub OnFileCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)
            Dim ConvertXml As Translator
            ConvertXml = New Translator()
            Dim XmlDoc As New XmlDocument
            Try
                XmlDoc = ConvertXml.Prepare(e.FullPath.ToString())
                XmlDoc.Save("c:\\dynamicsample.xml")
                System.Windows.Forms.MessageBox.Show(e.FullPath.ToString(), "", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question, System.Windows.Forms.MessageBoxDefaultButton.Button1, System.Windows.Forms.MessageBoxOptions.ServiceNotification)
                File.Delete(e.FullPath.ToString())
                '  MessageBox.Show("XML successful")
            Catch ex As Exception
                System.Windows.Forms.MessageBox.Show(ex.Message.ToString(), "", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question, System.Windows.Forms.MessageBoxDefaultButton.Button1, System.Windows.Forms.MessageBoxOptions.ServiceNotification)
            End Try
 
 End Sub 
Protected Overrides Sub OnStart(ByVal args() As String)
          
            IntializeFileSystemWatcher()
 End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I am Sorry, I misinterpreted my problem.
actually I wanted it to check for a file in the directory every 60 seconds.
Thank you
Use a timer object
The other option you could do is to use a thread and make the thread sleep for 60 seconds before it process next
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

If this is a Service Application you've created, it should be watching this directory all the time, for as long as the service is running.

I am not sure why you want the service to start/stop every 60 seconds? A timer inside your service will not fire, if the service is not already running. Thus, a timer could help you stop the service after 60 seconds, but not start it.

Author

Commented:
Hi NotLogical,
actually I misinterpreted my problem in my first question. Later I said that I need the FileSystemWatcher to look for a file every 60 seconds.
I am sorry if I created any confusion.
Thanks

Author

Commented:
spprivate,
Let me try with Thread as you suggested.
I am not sure what exactly you mean.  Are you saying I need to do something like as follows.

 Protected Overrides Sub OnStart(ByVal args() As String)
            Dim th As New Thread(AddressOf DoStart)
            th.IsBackground = True
            th.Start()
        End Sub
        Private Sub DoStart()
            IntializeFileSystemWatcher()
        End Sub
Hi ipjyo,

I did understand your added comment - thank you. I just wanted a little explanation, because FileSystemWatcher will give you an instantaneous notification when the file changes, is added or deleted or created. You correctly configured it for a creation event.

Did you want to delay the processing of the file by 60 seconds, or just process any of the files which are changed every 60 seconds?

I replicated your service here, and it seems to perform properly when a new file arrives into the "C:\Sample Data" directory...

Thanks for clearing this up!
Most Valuable Expert 2012
Top Expert 2014
Commented:
@ipjyo
The filesystemwatcher control generates events as soon as files are created/modified/deleted etc. If you want to process the files with an interval of 60 seconds, for whatever reason, you could use a Queue object to queue the files for processing in the filesystemwatcher events. Then use a Timer control and set its interval to 60 seconds. When the timer ticks, you could check the queue and if there are any files queued for processing, process them.

Author

Commented:
@NotLogical,
Thanks for the explanation.
I just wanted the FileSystemWatcher to delay file processing 60 seconds (process files every 60 seconds) instead of processing them as soon as they arrive.
Thank you.

@CodeCruiser,
I understand your suggested way of doing this. I will try this.
Thank you

Author

Commented:
@CodeCruiser,
Could you give me a sample template for queuing files if you have.
Thanks
Most Valuable Expert 2012
Top Expert 2014
Commented:

Author

Commented:
Thanks, I will go thru them.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial