fire an event at midnight

  • Thread starter Thread starter Jac
  • Start date Start date
J

Jac

Hey,

I have a windows service and when the day change (so at midnight) I want
fire an event so I can execute some code.

Do someone has an idea to do that?

I don't see directly a solution with a timer and an interval. Because
the first time then interval can not be 24hours? Is there something
easier?

thanks
jac
 
Unless you have "other things" going on, it seems like overkill to have a
Windows Service just to take up resources in order to fire one "event" at
midnight.

How about if you just write yourself a console Executable that does your
"thing" and set it up with Task Scheduler to run every day at midnight?
Peter
 
Jac said:
Hey,

I have a windows service and when the day change (so at midnight) I want
fire an event so I can execute some code.

Do someone has an idea to do that?

I don't see directly a solution with a timer and an interval. Because
the first time then interval can not be 24hours? Is there something
easier?

thanks
jac

One method but not so accuracy.

Using a timer with interval 1 minute (or period you select, e.g. 30sec,
15 sec, dependent on how accuracy you want).

Record the current time in every timer event by "DateTime.Now".

Compare the time in currnet timer event and previous timer event. If the
"Day" in the time has changed, execute the code you want.
 
Unless you have "other things" going on, it seems like overkill to have a
Windows Service just to take up resources in order to fire one "event" at
midnight.

How about if you just write yourself a console Executable that does your
"thing" and set it up with Task Scheduler to run every day at midnight?
Peter

Agree 100%
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Yes, You right but there is more in my program than that. It polls
constantly a database, but when it is week-end I want the interval for
polling mut be bigger, when it is monday then the interval must be
smaller. Therefore I need to now when the day changes.
But I already found a solution :
When the timer for polling elapse I will check date and then I can
change the interval.

Thanks
 
You could for example have the following code (NOTE: Not working, just off
the top of my head)

while(true)
{
If(CurrentDay != DateTime.Now.Day)
{
CurrentDay = DateTime.Now.Day;
//..Fire you code/Event here
}
}

Once again, this is unused untested and very resource hungry due to the fact
of the while loop.

However, I do believe that a safer option would be to use the Task scheduler
included with Windows, as this is a more proven and well tested component.

It could be worth having a read of:
http://blogs.msdn.com/dditweb/archi...es-Or-Task-Scheduler_3F00_-_2D00_-Part-I.aspx

http://www.codeproject.com/csharp/TSNewLib.asp

You could make the Windows Task Scheduler execute a application (unsure if
it can run DLL’s, however you could make a exe to use your DLL) every X time
during the day and also build in the ability to self modify its own schedule
based on what day it is.

Disclaimer: Any source code is posted without warranty or grantee and should
be used at your own risk.
 

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