I have a timer job that runs every minute. The timer job needs to do some processing and it may take more than a minute. I don't want the job to get triggered if previous instance is still running. That is, though it is supposed to run every minute, it should not run the next minute if previous one is still running. I am using SPJobLockType.Job as I need only single instance in farm. Please let me know if SharePoint will provide the desired behavior or if anything else is required to achieve this.
2 Answers
Yep, you're all good! Timer jobs rock, it's a pity we have to move away from them in the 'new school' style of SharePoint development
To check that whether your timer job is running, you can use "WebApplication.RunningJobs". look at following example,
private bool IsTimerJobRunning(string timerJobTitle, SPSite site)
{
return site.WebApplication.RunningJobs.Cast<Microsoft.SharePoint.Administration.SPRunningJob>().Any(job => job.JobDefinitionTitle.Equals(timerJobTitle));
}
if(IsTimerJobRunning("Your timer job name", spsiteObject))
{
// Timerjob is running
}
else
{
// timerjob is not running
}