I'm using .Net 4 to build a simple windows service , the problem happens when I want to make a 开发者_运维知识库5 min delay before starting the service.
protected override void OnStart(string[] args)
{
// make a 5 min delay
// do something
}
but after few seconds the service stops and gives me a time out error (saw this in the event-log). what am I suppose to do?
Start your long running process in a new thread, so don't block the OnStart
method. example:
using System.Threading;
protected override void OnStart(string[] args)
{
ThreadPool.QueueUserWorkItem(new WaitCallback((_) =>
{
Thread.Sleep(5 * 1000 * 60);//simulate 5 minutes work
// do something
}));
}
Dont. Services have to start when asked for. OnStart
HAS TO RETURN. FAST.
That said, the service then can wait 5 minutes until it DOES something, but you can not just stop the startup cycle.
Start it, then have the worker thread wait until it does something.
That said, there is a configuration entry that allows a service to be started with a delay. It is configured FOR the service, much like Manual, Automatic
there is a delayed
entry. This makes sure the service only starts when the machine has had a little time to settle down from starting. This may be better/good enough.
The OnStart
method needs to return in a timely fashion otherwise you will get a time out error.
What you need to do is start a thread from OnStart
and in that thread wait 5 minutes before actually starting your service. So your OnStart
method becomes:
serviceThread = new Thread(new ThreadStart(workerClass.WorkerMethod));
serviceThread.Start();
And WorkerClass.WorkerMethod
does the actual starting of the service.
A service has to respond when it's told to start, or Windows thinks its malfunctioning. So you have two options:
Write a seperate application that runs on startup, waits 5 minutes, and then starts the service.
Start the service, and put a five minute delay in it before it starts doing whatever it's business logic is. So long as Windows thinks it started up normally, it's happy.
Something like this:
private static Timer t;
protected override void OnStart(string[] args)
{
t = new Timer(5000*60);
t.Elapsed += new ElapsedEventHandler(OnTimedEvent)
t.Start()
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
// real code here
}
I don't understand why you want to delay your service start routine for 5 mins. if the service routine takes longer Service Control Manager probably aborts all the operation so your service can not start. You would rather need to start a thread/Timer inside OnStart Method and Wait for 5 mins on the Thread/Timer procedure.
protected override void OnStart(string[] args)
{
Thread processor= new Thread(ThreadProc);
processor.Start();
}
private void ThreadProc()
{
while (true)
{
Thread.Sleep(TimeSpan.FromMinutes(5));
}
}
Use a System.Timers.Timer to defer your actions... Here's some code from one of my services. The reason I use the defer is because windows assumes a service is broken if it takes too long to return from the onstart event. Since I do a lot of stuff when the service first starts I start the service very quickly with just the creation of a timer and then let that event deal with the real startup stuff.
protected override void OnStart(string[] args)
{
try
{
// Using a timer Event to start asynchronously
StartupTimer = new Timer();
StartupTimer.Elapsed += StartupTimer_Elapsed;
StartupTimer.AutoReset = false;
StartupTimer.Interval = 5*60*1000;
StartupTimer.Start();
}
catch (Exception ex)
{
LogMessage(true, ex.ToString());
}
}
private void StartupTimer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
RunSetup(true);
}
catch (Exception ex)
{
LogMessage(true, ex.ToString());
}
}
A straight forward way is to use System.Threading.Timer and set it's due time to 5 minutes, please see System.Threading.Timer for more information.
The root of the problem is that if the service takes along time to start Windows will think that it is malfunctioning and kill it.
You can fix this by first telling Windows that this service is going to take some time to start.
To do this see the answers of this SO question: windows service startup timeout
精彩评论