Stop execution of a scheduled task

Stop a scheduled task manually 

The following example shows how to use the Scheduling API to stop a scheduled task's next execution and delete it from the database:

C#
using Telerik.Sitefinity.Scheduling;

public partial class SitefinitySamples
{
   public void StopTask()
   {
       const string MyScheduledTaskName = "SitefinityWebApp.MyScheduledTask"; // change with your actual task name

       Telerik.Sitefinity.Scheduling.SchedulingManager schedulingManager = SchedulingManager.GetManager();

       Telerik.Sitefinity.Scheduling.Model.ScheduledTaskData task = schedulingManager.GetTaskData()
           .FirstOrDefault(x => x.TaskName == MyScheduledTaskName);

       if (task != null)
       {
           Telerik.Sitefinity.Scheduling.Scheduler.Instance.StopTask(task.Id);
           // Optionally, delete the task
           schedulingManager.DeleteTaskData(task);
           schedulingManager.SaveChanges();
       }
   }
}

Implement stopping logic

To stop the actual code execution of the scheduled task's ExecuteTask() method, you must implement custom logic to check if the task status is set to Stopped and return from the method early. This is needed because once a task starts its execution, there is not out of the box logic that forcefully stops it.

C#
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Scheduling;
using Telerik.Sitefinity.Utilities.Json;
using Telerik.Sitefinity.Data;

namespace SF142
{
   public partial class SampleScheduledTask : ScheduledTask
   {
       private bool ShouldStopExecution()
       {
           const string MyScheduledTaskName = "SitefinityWebApp.MyScheduledTask"; // change with your actual task name
           var transactionName = Guid.NewGuid().ToString();
           var schedulingManager = ManagerBase.GetManagerInTransaction(typeof(SchedulingManager), "OASchedulingProvider", transactionName) as SchedulingManager;        ScheduledTaskData taskData = schedulingManager.GetTaskData(this.Id);
           return taskData == null || taskData.Status == Telerik.Sitefinity.Scheduling.Model.TaskStatus.Stopped;
       }
   }
}

You may use this method in a scenario where the ExecuteTask() method's execution time is long or CPU intensive, for example:

C#
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Scheduling;
using Telerik.Sitefinity.Utilities.Json;

namespace SF142
{
   public partial class SampleScheduledTask : ScheduledTask
   {
       public override void ExecuteTask()
       {
           // Strictly example method implementation
           for (int i = 0; i < int.MaxValue; i++)
           {
               if (this.ShouldStopExecution())
               {
                   return;
               }
               Log.Write($"ExecuteTask() on {DateTime.UtcNow.ToShortTimeString()} ", ConfigurationPolicy.Trace);

               Thread.Sleep(2000);
           }
       }
   }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.