Application executed at given time

  • Thread starter Thread starter mamin
  • Start date Start date
M

mamin

I need to write application, which checks the time and executes some
method when the current time is 11pm.Moreover, I need to keep the time
parameter in App.config file. My question is, how to do it ellegant?
Now, my sourcecode looks as follows:

App.Config file:
<add key="ProcessKillingTime" value="23"></add>
Application.cs file:
while(1)
{
DateTime currentDate=System.DateTime.Now;
DateTime processKillingTime=new
DateTim(currentDate.Year,currentDate.Month,currentDate.Day,
processKillingTime,0,0);

if( processKillingTime.AddMinutes(30) > currentDate
&& processKillingTime.AddMinutes(-30) < currentDate)
{ code that should be executed at 23pm}
...here,waits 1 hour.
}
So, as you can see it's not very elegant. Any idea to do it better?
 
Well, in fact it is windows service. But the code I need to improve is:
DateTime currentDate=System.DateTime.Now;
DateTime processKillingTime=new
DateTim(currentDate.Year,currentDate.Month,currentDate.Day,
processKillingTime,0,0);


if( processKillingTime.AddMinutes(30) > currentDate
&& processKillingTime.AddMinutes(-30) < currentDate) {}
Don't you think that better way to do it exists?
 
mamin,

Don't use a windows service for this. Write your app as an executable
and then set it up as a scheduled task which runs at 11 PM. If you need to
change the time, then just change the scheduled task.

The problem with setting it up as a service is that the config file is
read once. Even if you change the time, the app won't know. .NET 2.0 has
new options in the config classes to reload the file, but then you have to
ask yourself, how often will you refresh the app config file?

It's a lot of extra processing for something that runs once a day. Just
set it up as a scheduled task.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top