1

The application I'm developing right now allows the user to update an Excel sheet or Sql database for set metrics twice a day. The program does this by popping up at certain times (e.g. 6:00 AM, 5:00 PM, 3:42 PM, whatever the user sets). By having the program pop up at certain times, the program ("Auto Excel It!!!") allows you as the user to track set data (say, sales calls, sales presentations, meetings, number of hours coding, number of jalepeño burritos eaten, etc.).

How can a developer get this program to "pop up"/start/function automatically at specific times through the means of the Windows Scheduler API (or something better)?

Here's how my understanding's evolved lately:

Nothing --> Use Timers As The Program Runs In The Background --> Use Windows Scheduler's API To Run Automatically (Current) --> Possible New Understanding From Your Answer

For example, I'm aware of: DispatcherTimers, Timers, another timer I'm not aware of, Sleep(), Windows Scheduler. But with these in mind, I don't know what to do regarding the following: Automatically starting a program via Windows Scheduler; Preserving computer resources if a timer is used; or even how to get this top pop up automatically.

Update 1:

@nfell2009:Your logic helped me out big time. At first I had to toy around with converting your Timer here to a DispatcherTimer (WPF forms standard, it seems). Then, I switched the the "Handles" for the Sub tCheckTime to "AddHandler tCheckTime.Tick, AddressOf tCheckTime_Tick" --- Why I had to do this is a good question.

Then, once I had the basic EventHandlers set up, your idea for comparing the user's text (As Date) to the System.Date is good--When I screwed something up and couldn't get the code to work, I switched it up and converted System.Date to a String--i.e. I went from String->Date To Date->String... That's when I got the Timer to work. When my System.Time ticked to 3:12 PM, the MsgBox popped up with "Your Message Here."

(A Quick (Evil) Thank You! I've spent four-plus hours getting this to work)

Code:

From Using "Handles" At tCheckTime_Tick (Which seems like it 'should' work)

Private Sub tCheckTime_Tick(sender As Object, e As EventArgs) Handles tCheckTime.Tick
...
End Sub

To AddHandler blah, AddressOf tCheckTime_Tick (Does work)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Loaded
        'MsgBox(Now().ToString("hh:mm"))        'String.Format("{hh:mm}", Now()))
        AddHandler tCheckTime.Tick, AddressOf tCheckTime_Tick 'Why is this necessary? 
        tCheckTime.Interval = New TimeSpan(0, 1, 0)
End Sub
Community
  • 1
  • 1
Aaron Bell
  • 852
  • 2
  • 11
  • 26
  • 1
    This is very wordy for such a simple question. You should aim to be to the point and concise. No need to butter us up or provide a backstory. – Corey Ogburn Dec 27 '13 at 16:47
  • possible duplicate of [How to call a method daily, at specific time, in C#?](http://stackoverflow.com/questions/3243348/how-to-call-a-method-daily-at-specific-time-in-c) – Corey Ogburn Dec 27 '13 at 16:51
  • I have a way of doing it, let me just test the code and I will answer it – BaeFell Dec 28 '13 at 14:39

2 Answers2

1
Public Class Form1
Dim iSetTime As Date
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub btnSetTime_Click(sender As Object, e As EventArgs) Handles btnSetTime.Click
    If (tCheckTime.Enabled = True) Then
        tCheckTime.Enabled = False
    End If

    iSetTime = txtTHour.Text + ":" + txtTMinute.Text + ":" + txtTSecond.Text

    tCheckTime.Enabled = True
End Sub

Private Sub tCheckTime_Tick(sender As Object, e As EventArgs) Handles tCheckTime.Tick
    If (TimeOfDay = iSetTime) Then
        MsgBox("Your Message")
    End If
End Sub
End Class

You will need error checking for the textboxs, but its simply: 3 textboxs with indication of which is which, so maybe a label each with H, M, S - or something.

A button which will set time and a timer. Naming: Textboxs Hours = txtTHour

Minutes = txtTMinute

Seconds = txtTSecond

Buttons Start Button = btnSetTime

Timers Timer = tCheckTime

BaeFell
  • 640
  • 1
  • 8
  • 28
  • Your logic helped me out big time. At first I had to toy around with converting your Timer here to a DispatcherTimer. Then, I switched the the "Handles" for the Sub tCheckTime to "AddHandler tCheckTime.Tick, AddressOf tCheckTime_Tick" (Why I had to do this... hmmm, good question). Once I had the basic EventHandlers set up, your idea for comparing the user's text (As Date) to the System.Date is good--When I screwed something up and couldn't get the code to work, I switched it up and converted System.Date to a string--comparing it to the code... That's when I got the Timer to work. – Aaron Bell Dec 28 '13 at 20:28
  • 1
    (Added Above Comment To The Update) – Aaron Bell Dec 28 '13 at 20:46
  • 1
    @crowbarProgramming if one of the answers answered your question you should mark it as the answer by clicking the checkmark next to the answer, that way they will get rep and you will too. Also it will mark the question as answered. – Mark Hall Dec 28 '13 at 20:50
  • Glad I could help and like @MarkHall said - Plus mark it as the answer :). Hope everything gets sorted now! :D – BaeFell Dec 28 '13 at 21:00
0

I can think of two easy ways:

  1. Have your program calculate the time until it should next appear in seconds and then set a timer with an elapsed time such that when the tick event is raised you can do whatever you need to do.

  2. Use MS Task Manager to launch your program when and as needed.

Rob
  • 3,488
  • 3
  • 32
  • 27
  • I would recommend option 2 (as suggested by Rob). – Pankaj Jaju Dec 27 '13 at 18:55
  • Regarding this, my knowledge of how to manipulate/access/use the MS Task Manager is somewhat limited--do you have any recommendations or sources for using it via VB? I'll Google what I can on it in the meantime. P.S. ... Thanks! – Aaron Bell Dec 28 '13 at 20:25