Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Date Time Programming Question

Posted on 2007-11-19
8
Medium Priority
?
208 Views
Last Modified: 2013-12-04
Ok, I am writing a program in VB.NET using Visual Studio and I need to incorporate code to make the program "aware" of what day it is. For instance, I want it to act differently (as in perform different tasks) on Monday than Tuesday etc. Also, I want it to act differently at 1:00PM than 1:00AM and so on. The program is running tasks continously and I need a way to keep a monitor running so as to check the time and "know" when to switch modes (perhaps asynch?). I am awarding 500 points not for difficulty but for speed and the numerous questions asked in one. Thank you.

pseudo code

if Monday then
do mondayProtocol

check time
if time > 1700 and <1800 then
do sixOClockRoutine

if time = 0000 then
run midnightBackup

etc
0
Comment
Question by:chosen19
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 20314455
You have:

    if Monday then
        do mondayProtocol

and your app is "running tasks continously"...so does that mean your app is also running continously?

It is technically Monday at midnight...so you want "mondayProtocol" to run when?

(a) At midnight on Monday
(b) Only once each Monday when the app is started
(c) Every time the app is started on Mondays
(d) Something else

What should happen if the app is not run on Monday at all?

Typically you have three choices for these types of apps:

(1) Use a Polling Timer and compare the current time against a list of events.
(2) Create a seperate thread for each event and put it to sleep until it should wake up.
(3) Use an external scheduling mechanism (such as windows scheduler) and pass an event "ID" into the app via command line arguments.
0
 

Author Comment

by:chosen19
ID: 20314638
My app is running continously and the Monday protocol would run all day Monday from 0000 until approximately 2300 when a subroutine would begin preperation for the Tuesday protocol. Also while running, the changing of the hour would change Monday subroutines.

a generic hierarchy would be

General Week Protocol (Normal Operations)
           Crossover         Crossover          Crossover    Crossover     Crossover       Crossover
Monday            Tuesday          Wednesday     Thursday         Friday             Saturday          Sunday
0000 Routine    0000 Routine   0000 Routine   0000 Routine   0000 Routine  0000 Routine    0000 Routine
0100 Routine    0100 Routine   0100 Routine   0100 Routine   0100 Routine  0100 Routine    0100 Routine
etc

with all the hourly routine (sorry I'm an mixing the terms routine and process/procedure) incrementing hourly so that theoretically each hour of each day has a seperate routine triggered - even if the routine calls for the app to do nothing.

I like the seperate thread idea, that seems to be the most logical for my situation as it would allow my crossover routine to work in the background while the other routines are running and would allow my program to continue it's foreground work. Could you elaborate with a specific code example on constructing the thread and setting it to initiate at a specific day time?
0
 
LVL 2

Expert Comment

by:andrewneely
ID: 20319328
Is this going to be a console app or a windows form?  If you are using a form, drop a timer control on the form.  Set its interval to 1000 milliseconds (i.e. 1 second).  Make sure that it is enabled (I say that becuase I forget that step from time to time :>).

Do some additional research on threading.  It is a very complicated issue (concurrency issues and the like)
The code I have provided I think will work, but I haven't done much threading.  

In the Timer tick event, you can use the following code

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
dim myDate as date = now
 Select Case mydate.DayOfWeek
            Case DayOfWeek.Sunday

            Case DayOfWeek.Monday
                'monday is the subroutine to which the thread will execute
                Dim myThred As New Threading.Thread(AddressOf monday)
                myThred.Start()
            Case DayOfWeek.Tuesday
            Case DayOfWeek.Wednesday
            Case DayOfWeek.Thursday
            Case DayOfWeek.Friday
            Case DayOfWeek.Saturday
        End Select
end sub
private sub monday()
 Select Case myTime
            Case Is = CDate("00:00:00")
               MidnightMonday
  end select
end sub
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20329812
The 'at' command in a command window (DOS Box) can do all the scheduling you need. Simply make a separate prog for each job and and let Windows do the scheduling.

Type

    at /?

or

    help at

at the command prompt if you want to learn more.

Regards, Alex
0
 
LVL 2

Accepted Solution

by:
andrewneely earned 1000 total points
ID: 20329865
You can also use teh scheduled tasks dialog from windows to do the same.  The only problem is that if you have many tasks set up, changing your password can be tedious.  We make batch files that update the password on every task. Here is a sample of the batch file.
@rem This program changes the password for the scheduled task to run under Windows Task Scheduler.
@rem If you add or delete tasks please change the list of tasks below.
@IF X%1==X GOTO Usage
@IF X%2==X GOTO Continue
@goto Usage

:CONTINUE
@cls
@schtasks /CHANGE /TN "Entry2Paf" /RU %USERNAME% /RP %1
@if %errorlevel%==0 goto CONT2
@goto BADINPUT

:CONT2
@schtasks /CHANGE /TN "Backup_db"          /RU %USERNAME% /RP %1
@goto done

:BADINPUT
@echo When attempting to change the first task in the batch
@echo the task scheduler returned an error (errorlevel = %errorlevel%).
@echo .
@echo The attempt to change the task to run under
@echo user %USERNAME% and password %1
@echo failed.
@echo .
@echo Skipping remaining changes
@goto done

:Usage
@Echo NewPass.bat is used to change the passwords for scheduled task
@Echo .
@Echo Syntax
@Echo    NewPass NewPassWord
@Echo .
@Echo Example
@Echo    NewPass Te$t1o
:Done
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 20387334
Maybe something like this?  (with more nested Select Case statements)

Imports System.Threading
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim T As New System.Threading.Thread(AddressOf SleeperThread)
        T.IsBackground = True
        T.Start()
    End Sub
 
    Private Sub SleeperThread()
        Dim dt As DateTime = DateTime.Now
        While True
            ' sleep until the top of the next hour...
            dt = DateTime.Now
            dt = New DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0).AddHours(1)
            Thread.Sleep(dt.Subtract(DateTime.Now).TotalMilliseconds)
 
            ' do something based on the current day and time...
            Select Case DateTime.Today.DayOfWeek
                Case DayOfWeek.Sunday
 
                Case DayOfWeek.Monday
 
                Case DayOfWeek.Tuesday
 
                Case DayOfWeek.Wednesday
 
                Case DayOfWeek.Thursday
 
                Case DayOfWeek.Friday
 
                Case DayOfWeek.Saturday
                    Select Case DateTime.Now.Hour
                        Case 0
                            Debug.Print("Saturday: 0000 hrs")
 
                        Case 1
                            Debug.Print("Saturday: 0100 hrs")
 
                        ' etc...
 
                    End Select
            End Select
        End While
    End Sub
 
End Class

Open in new window

0
 
LVL 1

Expert Comment

by:Computer101
ID: 21000173
Forced accept.

Computer101
EE Admin
0

Featured Post

Industry Leaders: 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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

636 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