Hi,
I have tried several ways to make this work but only found work arounds. so I am posting this as a last trial.
I have a windows service which has to kick start and generate a file and email it to our clients every monday and friday.
the service does this only when i stop it and start it. but it has to do this without someone starting it. i know we use a timer for this. i did too. but i dont know whats the issue. the timer is supposed to start everyday and check if its a monday or friday which it is not doing.
below is my code
-
namespace export
-
{
-
public partial class ExportService1 : ServiceBase
-
{
-
-
Timer _timer = new Timer();
-
ServiceController controller = new ServiceController();
-
-
public ExportService1()
-
{
-
_timer = new Timer(89000000);
-
InitializeComponent();
-
}
-
-
protected override void OnStart(string[] args)
-
{
-
-
_timer.Start();
-
newfunction(null);
-
}
-
protected override void OnContinue()
-
{
-
_timer.Start();
-
}
-
protected override void OnStop()
-
{
-
_timer.Start();
-
-
}
-
protected override void OnShutdown()
-
{
-
-
base.OnShutdown();
-
_timer.Stop();
-
-
}
-
public void newfunction(object obj)
-
{
-
-
Form fr = new Form();
-
fr.Export();
-
-
//HERE IT IS TAKEN TO ANOTHER CLASS WHERE IT CHECKS IF TODAY IS MONDAY OR FRIDAY AND REST OF THE WORK IS DONE.//
-
-
-
}
-
-
-
}
-
}
-
-
Thanks
Ayush
So when you stop and you start, the program works.
It doesn't work automatically each time the timer ticks off.
Then the timer isn't working as you think it should, in one respect or another.
I notice you say "The timer
starts every day." It really shouldn't. The timer should be running all the time. It doesn't 'start' every day; it 'Tick's every time the interval setting in milliseconds is reached. Saying that it 'starts' every day implies that it stops every day as well. If the timer stops, then something has to cause it to start again. Maybe this is the problem without going any further. Maybe your called method stops the timer and nothing starts it up again.
But, assuming that's not the case... Where else could it be performing differently than expected?
To start with, the interval for 24 hours should be 86,400,000 milliseconds.
You have 89000000 which is...
/1000ms = 890000 seconds
/60 = 1483.333 minutes
/60 = 24.722222 hours or nearly 3/4 of an hour longer than a day.
So if you are expecting your program to execute at the same time every day; it won't. Its going to creep by .7222 hours each time the timer ticks off 24.722 hours. After 4 ticks it will be off nearly 3 hours (2.888888). By the end of the 7 day week it will be off more than 5 hours. So maybe it would carry out as expected, if it were giving the extra time to reach its countdown. I presume when it doesn't work AT 24 hours you say to yourself [b]
"What the frack!"[/B] and kill it, then start it manually and it works fine.
There is no code shown for the method called when the timer actually ticks.
Should look something like...
- private void _timer_Tick(object sender, EventArgs e)
-
{
-
// This happens every 24.722222 hours
-
}
When you create the timer, you also don't seem to be assigning a method to the Tick event that is raised when those 89000000ms elapse.
Should look something like...
- _timer.Tick += new System.EventHandler(this._timer_Tick);
So maybe its not performing as expected, because you don't have a method telling it to perform, or you don't call that method when the timer elapses?
Regards,
tlhIn'toQ