I am wondering say I have a job that is executing and the windows service it is running in has been told to stop. How can I del开发者_开发技巧ay this and wait till all the jobs are finished and quartz.net has shutdown.
I only see like
scheduler.Shutdown();
and
scheduler.IsStarted
You can use API-Doc
public virtual void Shutdown(
bool waitForJobsToComplete
)
this tells Quartz.Net to wait until all jobs are completed, or API-Doc
virtual IList GetCurrentlyExecutingJobs()
In addition you should ask the os to wait for your service MSDN:
[ComVisibleAttribute(false)]
public void RequestAdditionalTime(
int milliseconds
)
otherwise Windows would kill your service after 20 seconds (depending on your system settings) Source.
To prevent a service from stopping shutdown, the SCM will only wait up to a limit for your service to stop itself. The default for this limit is 20 seconds (this value is in the registry key WaitToKillServiceTimeout() in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control)
Ahh, and don't forget about a system shutdown, windows does not like to wait...
If you are using/installed the service that comes with the distribution, by default the scheduler will wait until all jobs finish executing. You don't have to do anything. If you wrote your own service wrapper, use the Shutdown method that is mentioned in @Andreas' answer.
Because Shutdown()
is a Task
you need to wait until it is completed (See code bellow), otherwise the WindowsService main thread won't wait for Quartz to finish all its stuff, including uncompleted jobs.
Use the overload for Shutdown(waitForJobsToComplete = true)
to wait for jobs to be finished:
scheduler.Shutdown(waitForJobsToComplete = true);
while (!scheduler.IsShutdown)
{
//waiting for shutdown to be completed
Thread.Sleep(1000);
}
精彩评论