---
title: Scheduled tasks
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

List of examples:

### Creating a scheduled task

```csharp

// Creates a new scheduled task object
TaskInfo newTask = new TaskInfo();

// Sets the basic task properties
newTask.TaskDisplayName = "New task";
newTask.TaskName = "NewTask";
newTask.TaskAssemblyName = "CMS.WorkflowEngine";
newTask.TaskClass = "CMS.WorkflowEngine.ContentPublisher";
newTask.TaskSiteID = SiteContext.CurrentSiteID;
newTask.TaskEnabled = true;

// Creates the scheduling interval for the task
TaskInterval interval = new TaskInterval();

// Sets the interval properties
interval.Period = SchedulingHelper.PERIOD_DAY;
interval.StartTime = DateTime.Now;
interval.Every = 2;
interval.Days = new System.Collections.Generic.List<DayOfWeek>()
{
    DayOfWeek.Monday,
    DayOfWeek.Sunday,
    DayOfWeek.Thursday
};

// Assigns the interval settings to the task
newTask.TaskInterval = SchedulingHelper.EncodeInterval(interval);

// Calculates the first run time for the new task
newTask.TaskNextRunTime = SchedulingHelper.GetFirstRunTime(interval);

// Saves the scheduled task to the database
TaskInfo.Provider.Set(newTask);

```

[> Back to list of examples](#toc)

### Updating a scheduled task

```csharp

// Gets the scheduled task
// Provide the site ID to get site-related scheduled tasks. For global tasks, use zero (0) as the site ID.
TaskInfo updateTask = TaskInfo.Provider.Get("NewTask", SiteContext.CurrentSiteID);
if (updateTask != null)
{
    // Updates the task properties
    updateTask.TaskDisplayName = updateTask.TaskDisplayName.ToLowerCSafe();

    // Saves the updated task to the database
    TaskInfo.Provider.Set(updateTask);
}

```

[> Back to list of examples](#toc)

### Updating multiple scheduled tasks

```csharp

// Gets all enabled scheduled tasks whose code name starts with 'New'
var tasks = TaskInfo.Provider.Get()
                                .WhereStartsWith("TaskName", "New")
                                .WhereEquals("TaskEnabled", 1);

// Loops through individual tasks
foreach (TaskInfo task in tasks)
{
    // Updates the task properties
    task.TaskDisplayName = task.TaskDisplayName.ToUpper();

    // Saves the updated task to the database
    TaskInfo.Provider.Set(task);
}

```

[> Back to list of examples](#toc)

### Executing a scheduled task

```csharp

// Gets the scheduled task
// Provide the site ID to get site-related scheduled tasks. For global tasks, use zero (0) as the site ID.
TaskInfo runTask = TaskInfo.Provider.Get("NewTask", SiteContext.CurrentSiteID);

if (runTask != null)
{
    // Executes (runs) the task, regardless of its interval settings
    SchedulingExecutor.ExecuteTask(runTask);
}

```

[> Back to list of examples](#toc)

### Deleting a scheduled task

```csharp

// Gets the scheduled task
// Provide the site ID to get site-related scheduled tasks. For global tasks, use zero (0) as the site ID.
TaskInfo deleteTask = TaskInfo.Provider.Get("NewTask", SiteContext.CurrentSiteID);

if (deleteTask != null)
{
    // Deletes the scheduled task
    TaskInfo.Provider.Delete(deleteTask);
}

```

[> Back to list of examples](#toc)
