Schedule a task
Create the task
To create a scheduled task, perform the following:
- Create an instance of your scheduled task class.
- Set the execution time and other custom properties, if required. You can set the execution time in one of the following ways:
- Use the
ExecutionTimeproperty.
You sets the exact execution date. - Use the
ScheduleSpecproperty.
You describe the scheduled specification using the cron syntax.
When using this approach, you need to set theScheduleSpecTypeto"crontab".
- Use the
- To schedule the task, use the
SchedulingManager.
EXAMPLE: The following example demonstrates the use of cron syntax. For more information, seeGenerate crontab expressions.
C#using System; using Telerik.Sitefinity.Scheduling; namespace SitefinityWebApp { public partial class ScheduleTaskSnippets { public void ScheduleMyScheduleTask() { SchedulingManager manager = SchedulingManager.GetManager(); MyScheduledTask newTask = new MyScheduledTask() { ExecuteTime = DateTime.UtcNow.AddSeconds(10), CustomData = new MyScheduledTask.MyCustomData() { MyIntData = 3, MyStringData = "My string data" } }; // With cron syntax MyScheduledTask newCronTask = new MyScheduledTask() { ExecuteTime = DateTime.UtcNow, ScheduleSpec = "0 0 * * *", // The task will execute every day at midnight ScheduleSpecType = "crontab", CustomData = new MyScheduledTask.MyCustomData() { MyIntData = 3, MyStringData = "My string data" } }; manager.AddTask(newTask); manager.AddTask(newCronTask); manager.SaveChanges(); } } }
Start a scheduled task manually
You can also execute the scheduled task once using SchedulingManager.RescheduleNextRun() in your Sitefinity CMS widgets or event handlers:
C#
using System;
using System.Linq;
using Telerik.Sitefinity.Scheduling;
namespace SF142
{
public partial class SitefinitySamples
{
public static void SheduleTask()
{
DateTime executeTime = DateTime.UtcNow.AddHours(1);
// Create a new scheduled task
SchedulingManager schedulingManager = SchedulingManager.GetManager();
MyScheduledTask newTask = new MyScheduledTask()
{
ExecuteTime = executeTime
};
schedulingManager.AddTask(newTask);
SchedulingManager.RescheduleNextRun();
schedulingManager.SaveChanges();
}
}
}
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.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
Foundations of Sitefinity ASP.NET Core Development
The free on-demand video course teaches developers how to use Sitefinity ASP.NET Core and take advantage of its decoupled architecture and modern development model.