[2.0] Start a service at a predefined time.
Hi all,
I wrote a C# .Net service to automatically do some process in each 10 seconds. Say I start the service at 10:10:10 hours, then process are executed at 10:10:10, 10:10:20, 10:10:30 ......and so on until I stop the service.
But I want to start the service at 10 seconds interval. That is either on 00, 10, 20, 30... seconds time. Not like 11, 34, 56....
In other words, even I start the service manually at anytime my processing should start like times on 10:20:00, 2:45:30, 11:19:50, etc
I think it is clear for you now.
All the things I start from the constructor method. May following code segment helps you...
Code:
namespace RfService
{
public class RfService : System.ServiceProcess.ServiceBase
{
public RfService()
{
InitializeComponent();
const double INTERVAL = 10000;
TimerTicker = new System.Timers.Timer( INTERVAL );
TimerTicker.Elapsed += new ElapsedEventHandler( this.ServiceTimer_Tick );
}
private void ServiceTimer_Tick( object sender, System.Timers.ElapsedEventArgs e )
{
// Do the processing here
}
}
}
Can you guys and gals give me a help to do it.:o
Re: [2.0] Start a service at a predefined time.
When you start the service you should calculate the number of milliseconds until the next even 10 minutes and assign that value to your Timer's Interval. When the Elapsed event occurs it will on or about the right time, then you set the Interval to 600000 so the Elapsed event is raised every 10 minutes from then.
Re: [2.0] Start a service at a predefined time.
Ok, thanks. Before that I need a clue from you.
Actually you can see that my constructor method, where I start my time ticker by 10 seconds interval. Say I want to hold it 2 minutes before start execution the ServiceTimer_Tick(). I just want to do this only one time just after the service start.
Re: [2.0] Start a service at a predefined time.
Put a Thread.Sleep in the New method for the form and that will make it wait for however long before starting things up.
Re: [2.0] Start a service at a predefined time.
If you're using a Timer there's no need, or point, sleeping a thread. If you want a Timer to elapse at a specific time then you set the Interval so that it will. Don't set the Interval to 10000 unless you specifically want the Timer to elapse in 10 seconds. If you want the Timer to elapse in 2 minutes then set the Interval to 120000, which is the number of milliseconds in 2 minutes.
Re: [2.0] Start a service at a predefined time.
Yes jmcilhinney, I got the point. But the timer normally execute in predefined interval, as you said 10000 for 10 seconds and so on. What I want to do is, before the timer start or timer execute I want to hold the service 2 minutes. After first two minutes, I want to repeat the process in every 10 second interval.
I think it is clear to you.
Re: [2.0] Start a service at a predefined time.
Quote:
Originally Posted by Tom Sawyer
Put a Thread.Sleep in the New method for the form and that will make it wait for however long before starting things up.
But I really new for Thread and stuff. So that mean I have lots of work to do on this question. :(
Re: [2.0] Start a service at a predefined time.
Either you're not getting me or i'm not getting you. The Interval property of the Timer indicates the amount of time until the next Elapsed event. Nowhere does it say that this Interval value cannot change. If you want the first event raised two minutes after the service starts then the Interval should be 1200000 initially. If you want subsequent events raised ten seconds apart then you simply change the Interval to 10000 when the first event is raised.
Re: [2.0] Start a service at a predefined time.
Quote:
Originally Posted by jmcilhinney
Either you're not getting me or i'm not getting you. The Interval property of the Timer indicates the amount of time until the next Elapsed event. Nowhere does it say that this Interval value cannot change. If you want the first event raised two minutes after the service starts then the Interval should be 1200000 initially. If you want subsequent events raised ten seconds apart then you simply change the Interval to 10000 when the first event is raised.
No pal, I clearly get you and everything you told is clear enough to me.
What I'm say is, immediately after start my service I want to keep the 10 second process holds for 2 minutes. Right after execute that 2 minute, as my code done I want to processing the event every 10 seconds. So that 2 minute story comes to the picture only ones, just after the service start and thats it.
So, where I'm stuck is that how to handle that initial 2 minutes...
Re: [2.0] Start a service at a predefined time.
I'm still not 100% sure what you're asking for that isn't covered by what I've already said. Are you saying that you want one action perfromed after two minutes and then a different action performed every ten seconds thereafter?
Re: [2.0] Start a service at a predefined time.
Yep, exactly that's what I want.
Re: [2.0] Start a service at a predefined time.
So you do exactly what i've already said to do and just use a bool to tell you whether this is the first Elapsed event or not:
CSharp Code:
private bool firstElapsedEvent = true;
Your Elpased event handler would contain code along these lines:
CSharp Code:
if (this.firstElapsedEvent)
{
this.timer1.Interval = 10000;
this.firstElapsedEvent = false;
// This is the two minute event.
}
else
{
// This is a 10 second event.
}
Re: [2.0] Start a service at a predefined time.
Thanks pal. I got now what you say. So, you set the interval of first elapsed event and set the second one to use.
Anyway on line 3 now I got an error and working on it now. I'll be here if I couldn't have a luck.