Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js Asp.net Core C C#
      DotNet HTML/CSS Java JavaScript Node.js
      Python React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Task Scheduler in Asp.Net core

Task Scheduler in Asp.Net core

Date- Jul 20,2022

Updated Mar 2026

17518

Free Download Pay & Download
Scheduler Service Task Scheduler

Overview of Task Scheduling in ASP.NET Core

Task scheduling is a technique used to execute specific pieces of code at predetermined intervals. This concept is essential in web applications where certain tasks, such as sending emails, cleaning up databases, or generating reports, need to be performed regularly without manual intervention. By automating these tasks, developers can ensure that their applications run smoothly and efficiently, freeing up time to focus on enhancing user experiences.

ASP.NET Core provides a robust framework for implementing task scheduling. Leveraging the built-in IHostedService interface, developers can create background services that run in the context of the web application. This integration allows for better resource management and ensures that tasks run consistently, even when the application is under load.

In this article, we will walk through the process of creating a task scheduler in ASP.NET Core, examining the necessary components and providing practical examples to illustrate the implementation.

Prerequisites

Before diving into the implementation, ensure you have the following prerequisites:

  • A basic understanding of C# and ASP.NET Core.
  • Visual Studio or any other compatible IDE for ASP.NET Core development.
  • A new ASP.NET Core project set up and ready for development.

Creating the Scheduler Service

To create a task scheduler, we will define a new class that implements the IHostedService interface. This interface provides methods to start and stop background services. Below is a step-by-step guide to implement a simple scheduler service.

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace SchedulerService.Models {
    public class SchedulerService : IHostedService, IDisposable {
        private int executionCount = 0;
        private System.Threading.Timer _timerNotification;
        private readonly IServiceScopeFactory _serviceScopeFactory;
        private readonly IConfiguration _iconfiguration;

        public SchedulerService(IServiceScopeFactory serviceScopeFactory, IConfiguration iconfiguration) {
            _serviceScopeFactory = serviceScopeFactory;
            _iconfiguration = iconfiguration;
        }

        public Task StartAsync(CancellationToken stoppingToken) {
            _timerNotification = new Timer(RunJob, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
            return Task.CompletedTask;
        }

        private void RunJob(object state) {
            using (var scope = _serviceScopeFactory.CreateScope()) {
                try {
                    // Your scheduled task code here
                    Console.WriteLine($"Job executed {++executionCount} times.");
                } catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        public Task StopAsync(CancellationToken stoppingToken) {
            _timerNotification?.Change(Timeout.Infinite, 0);
            return Task.CompletedTask;
        }

        public void Dispose() {
            _timerNotification?.Dispose();
        }
    }
}

In this code, we define a timer that triggers the RunJob method every minute. You can adjust the interval by changing the TimeSpan.FromMinutes(1) parameter to suit your needs.

Registering the Scheduler Service

Once the scheduler service is created, the next step is to register it in the ASP.NET Core dependency injection container. This is done in the Startup.cs file within the ConfigureServices method. Here’s how you can do it:

public void ConfigureServices(IServiceCollection services) {
    services.AddHostedService<SchedulerService.Models.SchedulerService>();
    // Other service registrations
}

After adding this line, the ASP.NET Core runtime will automatically manage the lifecycle of the SchedulerService, ensuring it starts and stops appropriately with the application.

Implementing the Scheduled Task Logic

Within the RunJob method, you can implement the logic for the task you want to execute at regular intervals. This could be anything from querying a database to sending emails. Below is an example of sending a simple log message:

private void RunJob(object state) {
    using (var scope = _serviceScopeFactory.CreateScope()) {
        try {
            // Example task: Log execution time
            Console.WriteLine($"Task executed at {DateTime.Now}");
        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}

This example logs the current date and time each time the job runs, demonstrating how you can incorporate your application logic.

Edge Cases & Gotchas

When implementing a task scheduler, there are several edge cases and potential pitfalls to be aware of:

  • Long-Running Tasks: If the task takes longer than the interval set for the timer, overlapping executions may occur. To prevent this, consider using a flag or semaphore to ensure that the previous execution has completed before starting a new one.
  • Exception Handling: Ensure that exceptions within the RunJob method are properly handled. Uncaught exceptions can crash the timer, stopping subsequent executions.
  • Resource Management: Be cautious with resource-intensive tasks. If a scheduled job consumes too much memory or CPU, it may affect the overall performance of your application.

Performance & Best Practices

To ensure that your task scheduler performs optimally, consider the following best practices:

  • Adjust Timer Interval: Set the timer interval based on the expected execution time of your tasks. If tasks are quick, shorter intervals may be acceptable; otherwise, longer intervals can prevent overlap.
  • Use Asynchronous Programming: If your scheduled tasks involve I/O operations (like database calls or API requests), utilize asynchronous programming patterns to avoid blocking the thread.
  • Logging and Monitoring: Implement logging to track the execution of scheduled tasks. This can help diagnose issues and monitor performance over time.
  • Graceful Shutdown: Ensure that your service can handle application shutdown gracefully, allowing any running tasks to complete before stopping.

Conclusion

Implementing a task scheduler in ASP.NET Core is a straightforward process that can greatly enhance the functionality of your web applications. By automating recurring tasks, you can improve efficiency and reliability, allowing your application to perform critical operations without manual intervention.

  • Task scheduling: Essential for automating recurring jobs.
  • Using IHostedService: Provides a robust way to manage background tasks.
  • Handling edge cases: Important to avoid crashes and performance issues.
  • Best practices: Ensure optimal performance and reliability in task execution.
task scheduler

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

How to Encrypt and Decrypt Password in Asp.Net
May 15, 2022
Exception Handling Asp.Net Core
Aug 05, 2020
HTTP Error 500.31 Failed to load ASP NET Core runtime
Aug 23, 2022
How to implement Paypal in Asp.Net Core
Oct 30, 2022
Previous in ASP.NET Core
Model Validation In ASP.NET Core MVC 6.0
Next in ASP.NET Core
Asp.Net Core MVC Publish using FTP

Comments

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • Implement Stripe Payment Gateway In ASP.NET Core 16763 views
  • Send Email With HTML Template And PDF Using ASP.Net C# 16533 views
  • How to implement Paypal in Asp.Net Core 8.0 12902 views
  • Import data from Excel in Asp.Net 12759 views
  • HTTP Error 502.5 - ANCM Out Of Process Startup Failure 12746 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • Asp.net Core
  • C
  • C#
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor