If you need to run a background task within your Episerver website, then you'll likely create a scheduled task.  Scheduled tasks are a great way to run small bits of the functionality on a regular basis, like an import task etc..  If you have ever needed, or wondered how to enable/disable a scheduled task in code, then this guide is for you.  

Also, another quick note before I continue which seems to bring a lot of confusion.  Episerver scheduled tasks will run in the same application pool as your website.  If you need to consistently run a lot of processor heavy tasks, which will consume a lot of your website's resources then I recommend that you look into using something like Hangfire instead.  If you need to run 5-10 jobs that take a few minutes to run once an hour, then you don't need to worry.  You might want to consider something like Hangfire, to processes busy orders within an e-commerce store for example.

How Do I Access My Scheduled Tasks In Code

If you want to start messing around with scheduled tasks in code, you'll need to know about the IScheduledJobRepository interface. The IScheduledJobRepository API will give you access to do things like delete, get and update the current tasks registered with Episerver.  To use the IScheduledJobRepository code, you can access it like this. Using ServiceLocator in code is an anti-pattern and is written this way to make the code easier to read in this article. Use constructor injection in your solution.

Like a lot of things Episerver, the code is pretty simple. If you want to disable a task, you can do something like this:

Following this code, it's easy for you to start querying what jobs are running, disabling/enabling them where needed etc... There are also some useful properties on the scheduled task object, so you can query if they have been running.  Using the properties below, you could, for example, create a scheduled task that checked if all the other scheduled tasks ran successfully, if not, send an email for example.

If you look at your Episerver database, you can see the data for these tasks within the tblSchedule table.