Delete a job that may be running

Languages: C#, VB.NET

View on GitHub to download or comment.

See the Client API Examples Introduction for additional information and prerequisites.

Samples index

This sample demonstrates how to delete a job through the API.

If you attempt to delete a job that has running instances, you will get an exception. Before deleting a job you should terminate all running instances, as shown below.

sample.cs
public void DeleteJob(Job job)
{
    // if you want to make sure the job doesn't get executed while you're waiting for executing instances to complete, put it on hold
    job.UpdateHoldType(HoldType.DisableAll, MissedJobCheckOptions.NoCheck);

    // get the latest status for the job and check to see if there are running instances
    var status = job.GetStatus(true);

    if (status.ActiveInstances > 0)
    {
        // if there are running instances, prompt for confirmation before terminating if appropriate

        // if you want to continue deleting, terminate all active instances, and then wait until they have finished
        job.Terminate(TerminationOptions.TerminateAllInstances, 0);

        // refresh the status from the server once per second until all running jobs have been terminated
        while (status.ActiveInstances > 0)
        {
            System.Threading.Thread.Sleep(timespan.FromSeconds(1));
            status = job.GetStatus(true);
        }
    }

    // then you can delete the job
    job.Delete();
}
sample.vb
    sub DeleteJob(job As Job)
        'if you want to make sure the job doesn't get executed while you're waiting for executing instances to complete, put it on hold
        job.UpdateHoldType(HoldType.DisableAll, MissedJobCheckOptions.NoCheck)

        'get the latest status for the job and check to see if there are running instances
        Dim status=job.GetStatus(true)

        If status.ActiveInstances > 0 Then
            'if there are running instances, prompt for confirmation before terminating if appropriate

            'if you want to continue deleting, terminate all active instances, and then wait until they have finished
            job.Terminate(TerminationOptions.TerminateAllInstances, 0)

            'refresh the status from the server once per second until all running jobs have been terminated
            While status.ActiveInstances>0
                System.Threading.Thread.Sleep(timespan.FromSeconds(1))
                status=job.GetStatus(True)
            End While

        End If

        'then you can delete the job
        job.Delete()
    End sub

Comments

View on GitHub to comment.