We help IT Professionals succeed at work.

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

ToolTimeGang asked
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?
Watch Question

Windows Task Scheduler.
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
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


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!
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Haha!  I do the same thing sometimes...

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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.