execute a function based on a time setting

Posted on 2009-02-20
Last Modified: 2012-05-06
I have a simple windows service in I need to execute a function at a set time of the day. I have an existing process but its not stable. So say the time is set to 03:00, as soon as it gets to 3 or just over i want to execute a function once for that day, very similar to using the scheduled task manager.

Question by:CraigLazar
    LVL 1

    Expert Comment

    Why not use the windows task manager? You can right a simple exe to call from a scheduled task that all it does is call the function you want in your windows service.
    LVL 48

    Expert Comment

    You just need to use a timer and define a intervar = 1000 (one minute).
    In each timer elapsed event you check if the timer is 3:00 and do the function.
    LVL 85

    Accepted Solution

    Just make a Thread and put it to sleep for the required amount of time:

            Dim T As New System.Threading.Thread(AddressOf SleepingThread)

    With SleepingThread being:

        Private Sub SleepingThread()
            Dim dt As DateTime = DateTime.Today.AddHours(3)
            If DateTime.Now.Subtract(dt).TotalMilliseconds > 0 Then
                dt = dt.AddDays(1) ' it's past 0300...schedule it for tomorrow
            End If
            ' it's 0300 something:

            ' Execute your job here

        End Sub
    LVL 4

    Author Closing Comment


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    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.

    734 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

    23 Experts available now in Live!

    Get 1:1 Help Now