I need to make an application that performs a task at a given time each day, based on the system time. For a simple example, lets say I want to delete all the files in a folder at 11:00 am everyday. The program would obviously be running on a computer thats always on. What is the best way to do this Do I continuously compare the current time with the target time & when they're equal fire the event This seems like a waste of cpu resources, but maybe thats the only way to do it. Can somebody post some code to get me started on this Thanks...

Fire event at predetermined time
GARussell
Renee ... Thanks for the code, it works great. However, I don't understand the first line:
Protected Friend WithEvents tmr As New Timer
Can you explain what this line does (actually just the Protected Friend WithEvents part) Why can't you just say Dim tmr As New Timer Thanks again...
Dieter Depuydt
Hi,
Off the top of my head you could do this:
a = Time to fire event 11.00
b = Current Time 7.00 (Now)
c= Difference between a and b = 4
set the timer interval to be: .interval = 1000 x 60 x 60 x c (4 hours)
The above will fire the event 4 hrs from the current time of 7.00 which is 11.00
Hope this helps
Ron
If at first you don't succeed try try again!!!
Malicious User
Public Class Form1
Protected Friend WithEvents tmr As New Timer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
tmr.Interval = 60000 ' once a minute
tmr.Enabled = True : tmr.Start()
End Sub
Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
If Now.TimeOfDay.ToString.Contains("17:00") Then MsgBox("it's time to go home")
End Sub
End Class
JPR7
Shouldn't that be:
If Now.TimeOfDay.ToString.Contains("14:00") Then MsgBox("it's time to go home")
Yay!!!
MikeC23
Yo!
Stop being sooooo charming.
CPark72
My solution:
1.) I wrote a WSH VBS Script, which compresses all files in a folder, move this folder to a backup folder and then deletes all files in the original folder.
2.) I used the commandline tool "at" to put in a task for the system scheduler.
As far as I heard, this little piece of software is still running and works just fine. And my former company is still happy with this solution.
Pros:
- Easy to maintain and to check if it worked, since no compiled program is used, so you can change the folders, tools etc. very easily. And the Task Scheduler keeps track of the runs!
- You use a proven timing mechanism from the OS; As far as I know the System Task Scheduler is working without any reported problems since the good old days of Win NT 4.0 or maybe earlier Versions.
- You can apply different user settings. The task can run as Admin, if it need to have special rights, or you can create a special user for it!
- You don't write another program that wastes processing power/memory only to wait for a time to appear. Even with the suggested timeout, it still sitting in the memory and waiting! And I guess there are thousend out in the wild, already! For example this stupid Sony Rootkit, which if it runs, checks every 5 sec if you steal music (F**k Sony!)! Or the other stupid Update Checkers almost every company nowadays put in the Run Registry, to check if there is a new version available. Which in my eyes is the stupiest solution, you can imagne.
[SUGGESTION TO MICROSOFT: Put a Service in the OS, where you can register any software or file, the current version, a URL, and a time/date to check, and the OS informs the user when new versions is available to download/install, so the user can deceide what to do; instead of being ambushed everytime a program thinks it must be updated; IMHO its better to have a MS controlled service, than having dozends of programs checking the time and date, to perform an update check]
- It looks more professional than the other solution suggested! Nervertheless I agree with going home at 2pm :)
- ... add your on pros
Cons:
- You have to use WSH VBS, but I guess you can write a VB.Net program to delete entries in a folder or whatever task the program should perform. For that matter I guess you could use any language, which is capable to delete files.
- You miss the fun using VB.Net (Suggestion to MS rename it to B# :) or C# or J# or C++ or whatever programming language you prefer.
- ... add your on cons
Hope it helps,
BavBoy
PS: Sorry, no code available, since I don't own the rights to my glorious solution :(