Hello all,
This is my first post and I have to admit that I am a newbie when it comes to programming in C#. With that being said, please forgive any cluelessness on my part ;)
Let me first state what I am trying to accomplish and then I'll ask my question. I'm trying to create a windows service that will run an event at a scheduled time, similar to the windows task scheduler (which I don't want to use). For example; I want the service to launch notepad every Wednesday at 4PM.
Now for the question. Would this better better suited for a Timer or running a loop that will check the DateTime every minute and compare it to the scheduled time? I would think running a loop like this would eventually cause an unnecessary processor load, but I'm not completely sure how C# manages memory.
FYI... In it's end state, I would like an administrator to be able to change the scheduled time using a separate form or ASP page, so I wouldn't want the code to call a static time like Wednesday at 4PM where a timer is just set with an interval of a week. I'd like the code to be open to the possibility that the admin would want the task to run on multiple days of the week. This is why I'm leaning more toward using a loop that will check DateTime, but I'm leaving it to the experts to guide me :)
Thanks in advance!
Scriptd
Ryan AlfordPosted Oct 28, 2008, 3:43 PM
class Program
{
static void Main(string[] args)
{
DateTime now = DateTime.Now;
Console.WriteLine(string.Format("The current date is: {0}", now));
Console.WriteLine(string.Format("The day of the week is: {0}", now.DayOfWeek));
Console.WriteLine(string.Format("The hour is: {0}", now.Hour));
Console.WriteLine(string.Format("The minute is: {0}", now.Minute));
Console.ReadLine();
}
}
you will notice that the "now.Hour" actually returns the hour in 24-hour format. So 3PM would return as 15.
so let's go by an example. Let's say you want to run this on Wednesdays at 5:30PM.
DateTime now = DateTime.Now;
// you would read these from the app.config file
string dayOfWeek = "Wednesday";
int hour = 17;
int minute = 30;
if (dayOfWeek == now.DayOfWeek)
{
if (now.Hour == hour)
{
if (now.Minute == minute)
{
// do your code
}
}
}
Scott FurrPosted Oct 28, 2008, 3:51 PM
Awesome!
Thanks a lot, you've been a great help!
Scott FurrPosted Oct 27, 2008, 9:09 PM
This is definitely helping me get on the right track but I'm looking for a code example of how to check for the days, hours, and minutes. All the examples I've seen show years and months and I really don't want that. Can I just eliminate those two options and go straight for the days, hours and minutes?
I REALLY appreciate all the help so far! Thanks!
Ryan AlfordPosted Oct 27, 2008, 8:56 PM
Scott FurrPosted Oct 27, 2008, 5:01 PM
I was thinking of something almost exactly along those lines. So for part 2 of my problem...
I know how to get the current time, but how do I SET a DateTime to compare it so that the service knows that the task is due? I read on another forum that you can't set it, that it's read only in a sense. Am I off base here?
Ryan AlfordPosted Oct 27, 2008, 4:54 PM