Run code from windows service at a particular time

Posted on 2011-02-25
Last Modified: 2012-05-11
Hi All

Is it possible to run code from inside a windows service at a particular time of day. I'm getting the time to run from a config file, and want to check for a file in a folder at that time.

I think I can do it by running the routine and storing the time, then use a timer to elapse until the next due time, but it would be so much easier to ask it to run at said time.

I cant use the scheduler.

Question by:Andy Green
  • 3
  • 2
LVL 29

Expert Comment

by:Paul Jackson
ID: 34979000
You can write your own windows service using

Have a look here :
LVL 11

Expert Comment

ID: 34979059
Have a thread that is running for each minute or for each x time. This thread should call the function you have and have a check in your function if the time is the same as your configured time.

Author Comment

by:Andy Green
ID: 35004487
Still stuck with this, My searches suggest using Thread.sleep, but I cant find any suitable code examples.

Any more thoughts anyone.

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

LVL 11

Accepted Solution

srikanthreddyn143 earned 500 total points
ID: 35007767
Yes you need to use that. Your code should be something like this.

In service OnStart() function

Call RunServiceThread()

Public Sub RunServiceThread()

Dim ServiceThread As New thread(Address Of TestFunc)

End Sub

Public Sub TestFunc()

 Do While True
                Thread.CurrentThread.Sleep(1000 * 60 * 2)  ' runs for every 2 minutes

If your condition then
Call your function.
End If

End Sub
LVL 11

Expert Comment

ID: 35007771
You need to handle errors for threading.

Author Closing Comment

by:Andy Green
ID: 35038305


Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now