Automate Tasks in .NET 8 Using Quartz and Cron Triggers

Written by hacker95231466 | Published 2025/11/10
Tech Story Tags: c-sharp-programming | quartz | scheduling-cron-jobs | net8 | .net-8-automation | quartz-scheduler | cron-trigger | c-job-scheduling

TLDRThis tutorial walks developers through automating recurring tasks in .NET 8 using Quartz Scheduler and Cron Triggers. You’ll learn how to define jobs and triggers in XML, schedule file-reading tasks that run every few seconds, and start a fully functional automation service that works on Windows or Linux. The guide highlights how Quartz simplifies timed executions and code-free updates for any repetitive workflow.via the TL;DR App

As a developer, you probably have tasks you’d like to automate — things like sending emails, generating reports, or cleaning up data on a schedule.

So, how do you make that happen?

That’s where services come in. They’re a great way to handle automation efficiently and reliably.

Let’s break down how you can set this up using services.

Introduction

Quartz is a popular open-source tool for scheduling and automation.

At the heart of it is the Quartz Trigger, a core component of the Quartz Scheduler, a powerful job scheduling library available in both C# and Java.

Official Documentation: https://www.quartz-scheduler.org/documentation/

What is a trigger?

A Trigger defines when and how often a job should run.
Think of it as theschedule attached to a job.

When you schedule a job in Quartz, you provide:

  • A Job (what to do)
  • A Trigger (when to do it)

Types of Quartz Triggers

  1. Simple Trigger
  • Runs a job a specific number of times or at fixed intervals.
  • Example: run every 10 seconds, repeat 5 times.

  1. Cron Trigger
  • Uses a Cron expression (like in Unix/Linux Cronjobs) for more complex schedules.
  • Example: run every day at 2:00 AM.

What is a Cron expression?

A Cron Expression is a string with 6 or 7 fields that defines a schedule, specifying exactly whena job should run (down to seconds).
It’s like acompact language for time-based scheduling.

Detailed expression: you can read it in their official documents.

The requirement is to read the file every 30 seconds.

Let’s implement a Cron trigger in .NET 8.

Project Settings

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <RootNamespace>CronImplementation</RootNamespace>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Quartz" Version="3.15.1" />
    <PackageReference Include="Quartz.Plugins" Version="3.15.1" />
  </ItemGroup>

	<ItemGroup>
		<Content Include="jobs.xml" CopyToOutputDirectory="Always" />
	</ItemGroup>
</Project>

Program

using Quartz;
using Quartz.Impl;
using Quartz.Xml;
using Quartz.Simpl;  // <-- needed for SimpleTypeLoadHelper

namespace CronImplementation
{
    class Program
    {
        static async Task Main(string[] args)
        {
            //Create a scheduler
            ISchedulerFactory factory = new StdSchedulerFactory();
            IScheduler scheduler = await factory.GetScheduler();

            //Create a type load helper (required by the new API)
            var typeLoadHelper = new SimpleTypeLoadHelper();
            typeLoadHelper.Initialize();

            //Use the new XMLSchedulingDataProcessor constructor
            var xmlProcessor = new XMLSchedulingDataProcessor(typeLoadHelper);

            //Ensure file path is an absolute
            string xmlPath = Path.Combine(AppContext.BaseDirectory, "jobs.xml");
            xmlProcessor.ProcessFileAndScheduleJobs(xmlPath, scheduler);

            Console.WriteLine(File.Exists(xmlPath));

            //Start the scheduler
            await scheduler.Start();

            Console.WriteLine("Quartz Scheduler started using XML configuration. Press any key to stop...");
            Console.ReadKey();

            await scheduler.Shutdown();
            Console.WriteLine("Scheduler stopped.");
        }
    }
}

File Reading Job

using Quartz;
 
namespace CronImplementation
{
    public class FileReadingJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            string filePath = Path.Combine(AppContext.BaseDirectory, "file.txt");

            if (!File.Exists(filePath))
            {
                Console.WriteLine($"File not found: {filePath}");
                return Task.CompletedTask;
            }

            //Read all lines
            string[] lines = File.ReadAllLines(filePath);

            foreach (var line in lines)
            {
                Console.WriteLine(line);
            }
            Console.WriteLine($"FileReadingJob executed at: {DateTime.Now}");
            return Task.CompletedTask;
        }
    }
}

Jobs.xml

<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     version="2.0">

	<!-- List of scheduled jobs -->
	<schedule>

		<!-- Job definition -->
		<job>
			<name>FileReadingJob</name>
			<group>group1</group>
			<description>Job that reads content from file</description>
			<job-type>CronImplementation.FileReadingJob, CronImplementation</job-type>
			<!-- Namespace.ClassName, Assembly -->
			<durable>true</durable>
			<recover>false</recover>
		</job>

		<!-- Trigger definition (Cron-based) -->
		<trigger>
			<cron>
				<name>FileReadingJobTrigger</name>
				<group>group1</group>
				<job-name>FileReadingJob</job-name>
				<job-group>group1</job-group>
				<cron-expression>0/10 * * * * ?</cron-expression>
				<!-- Runs every 10 seconds -->
			</cron>
		</trigger>

	</schedule>
</job-scheduling-data>

File.txt

“This is the console app. This project explains the implementation of the Cron trigger in NET 8.”

Output

Summary

The Quartz trigger is platform-independent, meaning you can deploy your apps on both Windows and Linux servers without any issues.

Another great feature is that it lets you assign different trigger schedules to multiple jobs, giving you more flexibility in how tasks are executed.

You can also update or delete triggers by simply editing the XML file—no need to change the code. Just keep in mind that the service needs to be restarted for the changes to take effect.


Written by hacker95231466 | Healthcare Architect. Develop applications in C#.Net. Java , Python ,Typescript & SQL in both cloud native and on Prem servers.
Published by HackerNoon on 2025/11/10