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

Posted on 2012-08-16
Medium Priority
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
  • 2

Assisted Solution

Andrew_Ellis earned 1000 total points
ID: 38300724
Windows Task Scheduler.
LVL 86

Accepted Solution

Mike Tomlinson earned 1000 total points
ID: 38301127
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

ID: 38301507
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 86

Expert Comment

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

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 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