how can I set up code to run during a specific time range?

Posted on 2012-08-16
Last Modified: 2012-08-16
I have a simple vb2010 exe that I want to fire up before I leave work on Friday.  I want it to sit in a hold mode and only run on Saturday for 30 minutes.

HOw can I do this?
Question by:ToolTimeGang
    LVL 1

    Assisted Solution

    Windows Task Scheduler.
    LVL 85

    Accepted Solution

    Why do you need it to sit open until Saturday?  If you use the Task Scheduler, as Andrew suggests, then you can have it open by itself on Saturday.

    Here's an approach, though, based on your description:
    Public Class Form1
        Private TargetDateTime As DateTime
        Private ShutDownDateTime As DateTime
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ' Start with today's date:
            Dim TargetDateTime As DateTime = DateTime.Today
            ' Get the next Saturday:
            While TargetDateTime.DayOfWeek <> DayOfWeek.Saturday
                TargetDateTime = TargetDateTime.AddDays(1)
            End While
            ' Pick a specific Time on that Saturday:
            ' Example --> 3:30 P.M.
            TargetDateTime = TargetDateTime.Add(New TimeSpan(15, 30, 0)) ' <-- hours are in 24 hour format!
            ' If the current time is already past the target time for that Saturday, set it up for next week's Satuday Date/Time:
            If DateTime.Now.Subtract(TargetDateTime).TotalMilliseconds > 0 Then
                TargetDateTime = TargetDateTime.AddDays(7)
            End If
            ' Setup the Timer() Interval so it fires at the target DateTime:
            Timer1.Interval = TargetDateTime.Subtract(DateTime.Now).TotalMilliseconds
            ' Setup the ShutDown DateTime for 30 minutes after the Target DateTime:
            ShutDownDateTime = TargetDateTime.AddMinutes(30)
        End Sub
        Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
            ' Are we past the shut down time?
            If DateTime.Now.Subtract(ShutDownDateTime).TotalMilliseconds > 0 Then
                Me.Close() ' shut down the application
                ' ... not past the shut down time so it must be time to do some work ...
                ' Change the Interval for the ShutDown DateTime:
                Timer1.Interval = ShutDownDateTime.Subtract(DateTime.Now).TotalMilliseconds
                ' ... start your work in here ...
                ' Use the BackgroundWorker() control to fire the work off in a background thread
            End If
        End Sub
    End Class

    Open in new window


    Author Closing Comment

    I feel silly I didn't think of the task scheduler on my own!  
    I will use the same code as well.
    Thank you both very much!
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Haha!  I do the same thing sometimes...

     "If all you have is a hammer, everything looks like a nail":

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    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

    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…
    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…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 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

    8 Experts available now in Live!

    Get 1:1 Help Now