• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

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

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?
  • 2
2 Solutions
Windows Task Scheduler.
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

ToolTimeGangAuthor Commented:
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 TomlinsonMiddle School Assistant TeacherCommented:
Haha!  I do the same thing sometimes...

 "If all you have is a hammer, everything looks like a nail":
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now