I'm working with a custom timer job and a custom ASPX page allowing end users to change the schedule for when the job runs. However, before they change or delete a job, I want to ensure the job is not running first. Here's the code on how I did this:
SPSite site = SPContext.Current.Site;
bool IsJobRunning = false;
foreach (SPRunningJob job in site.WebApplication.RunningJobs)
{
if (job.JobDefinitionTitle == "JobName")
{
IsJobRunning = true;
}
}
if (IsJobRunning)
{
// show an error message of some sort
}
else
{
site.WebApplication.JobDefinitions["JobName"].Delete();
}
Not rocket science, but seemed interesting enough to post…
Phil