Get current date everyday for a Windows service
I want to run my windows service every day on only a certain time so I did the following but how do I replace t开发者_开发技巧his date in every day?
Dim checkTime As DateTime
checkTime = checkTime.Parse("7/23/2010 3:41 PM")
If you just want it to have today's date and check for exactly 3:41PM you can do:
DateTime checkTime = DateTime.Now.Date.AddHours(15).AddMinutes(41);
You have the C# tag but your code is in VB so the VB equivalent is:
Dim checkTime As DateTime = DateTime.Now.[Date].AddHours(15).AddMinutes(41)
Does your program need to be running at all times, or does it simply need to execute a function at a specific time and when done, quit?
If the latter, you could simply use Windows' Task Scheduler to schedule your EXE to run on a regular basis.
If the former, there's still no need to reinvent the wheel. Take a look at Quartz.NET. It is an extremely robust job scheduling library that allows you to schedule jobs to run on a regular basis. Basically, you create a class inheriting from IJob
and its Execute()
method will be called by Quartz at the appointed time(s).
Word of warning: Quartz is multi-threaded and your IJob
class' Execute()
will be called on a thread separate from your main thread, so you're thus exposed to the dangers of threading.
精彩评论