Late4dTrain.CronTimer 0.0.1

dotnet add package Late4dTrain.CronTimer --version 0.0.1
                    
NuGet\Install-Package Late4dTrain.CronTimer -Version 0.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Late4dTrain.CronTimer" Version="0.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Late4dTrain.CronTimer" Version="0.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Late4dTrain.CronTimer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Late4dTrain.CronTimer --version 0.0.1
                    
#r "nuget: Late4dTrain.CronTimer, 0.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Late4dTrain.CronTimer&version=0.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Late4dTrain.CronTimer&version=0.0.1
                    
Install as a Cake Tool

CronTimer

CronTimer is a simple and lightweight library for creating cron jobs in .NET. It provides functionality similar to System.Timer but uses cron expressions to schedule tasks.

Features

  • Schedule tasks using cron expressions.
  • Supports both synchronous and asynchronous operations.
  • Integrates with Microsoft.Extensions.Logging for logging.
  • Supports dependency injection.

Installation

You can install the CronTimer library via NuGet:

dotnet add package Late4dTrain.CronTimer

Usage

Basic Usage

Here's a basic example of how to use the CronTimer:

using System;
using System.Threading;
using System.Threading.Tasks;
using Late4dTrain.CronTimer;
using Late4dTrain.CronTimer.Events;

class Program
{
    static async Task Main(string[] args)
    {
        var timer = new CronTimer("*/1 * * * * *", CronFormats.IncludeSeconds);
        timer.TriggeredEventHandler += async (s, e) => await HandleCronTimer(e);
        timer.Start();

        // Let the timer run for 7 seconds
        await Task.Delay(TimeSpan.FromSeconds(7));
        await timer.StopAsync(CancellationToken.None);
    }

    private static Task HandleCronTimer(CronEventArgs e)
    {
        if (e.CancellationToken.IsCancellationRequested)
            return Task.CompletedTask;

        return Task.Run(() => { Console.WriteLine("Task has completed at {0}.", DateTime.UtcNow); });
    }
}

Using Dependency Injection and Logging

To use CronTimer with dependency injection and logging, follow these steps:

  1. Add the necessary NuGet packages:

    dotnet add package Microsoft.Extensions.Logging
    dotnet add package Microsoft.Extensions.DependencyInjection
    
  2. Configure the services in your Startup class:

    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLogging(configure => configure.AddConsole());
    
            services.AddSingleton<ITimeProvider, SystemTimeProvider>();
            services.AddSingleton<IDelayProvider, SystemDelayProvider>();
    
            services.AddTransient<CronTimer>(provider =>
            {
                var logger = provider.GetRequiredService<ILogger<CronTimer>>();
                var timeProvider = provider.GetService<ITimeProvider>();
                var delayProvider = provider.GetService<IDelayProvider>();
    
                return new CronTimer(options =>
                {
                    options.AddCronTabs(new CronTab("*/5 * * * * *", CronFormats.IncludeSeconds));
                }, timeProvider, delayProvider, logger);
            });
        }
    }
    
  3. Use the CronTimer in your application:

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    
    class Program
    {
        static async Task Main(string[] args)
        {
            var serviceProvider = new ServiceCollection()
                .AddLogging(configure => configure.AddConsole())
                .AddSingleton<ITimeProvider, SystemTimeProvider>()
                .AddSingleton<IDelayProvider, SystemDelayProvider>()
                .AddTransient<CronTimer>(provider =>
                {
                    var logger = provider.GetRequiredService<ILogger<CronTimer>>();
                    var timeProvider = provider.GetService<ITimeProvider>();
                    var delayProvider = provider.GetService<IDelayProvider>();
    
                    return new CronTimer(options =>
                    {
                        options.AddCronTabs(new CronTab("*/5 * * * * *", CronFormats.IncludeSeconds));
                    }, timeProvider, delayProvider, logger);
                })
                .BuildServiceProvider();
    
            var timer = serviceProvider.GetRequiredService<CronTimer>();
            timer.TriggeredEventHandler += async (s, e) => await HandleCronTimer(e);
            timer.Start();
    
            // Let the timer run for 7 seconds
            await Task.Delay(TimeSpan.FromSeconds(7));
            await timer.StopAsync(CancellationToken.None);
        }
    
        private static Task HandleCronTimer(CronEventArgs e)
        {
            if (e.CancellationToken.IsCancellationRequested)
                return Task.CompletedTask;
    
            return Task.Run(() => { Console.WriteLine("Task has completed at {0}.", DateTime.UtcNow); });
        }
    }
    

License

This project is licensed under the MIT License. See the LICENSE file for more details.

This README.md provides an overview of the CronTimer library, installation instructions, basic usage examples, and instructions for using dependency injection and logging.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.1 135 9/17/2024
0.0.1-pre 98 9/17/2024

Still pre 1.0 version.