Skip Ribbon Commands Skip to main content

SharePoint Happenings

Help (new window)
Sign In
Navigate Up
Get Microsoft Silverlight
Install Silverlight plugin for a richer experience...
Blog Home |  Freeware |  Speaking |  About me

Checking if a Timer Job is running before you delete it…



Bookmark and Share

Top Tech Links










Top SharePoint Administrator Links









Top SharePoint Developer Links













NOTE: This blog has been moved to http://blog.philwicklund.com. If you'd like assistance, leave a comment on the copied post in the new location (hint, use search on the right to find the post).


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