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.
- Schedule tasks using cron expressions.
- Supports both synchronous and asynchronous operations.
- Integrates with Microsoft.Extensions.Logging for logging.
- Supports dependency injection.
You can install the CronTimer library via NuGet:
dotnet add package Late4dTrain.CronTimerHere's a basic example of how to use the CronTimer:
usingSystem;usingSystem.Threading;usingSystem.Threading.Tasks;usingLate4dTrain.CronTimer;usingLate4dTrain.CronTimer.Events;classProgram{staticasyncTaskMain(string[]args){vartimer=newCronTimer("*/1 * * * * *",CronFormats.IncludeSeconds);timer.TriggeredEventHandler+=async(s,e)=>awaitHandleCronTimer(e);timer.Start();// Let the timer run for 7 secondsawaitTask.Delay(TimeSpan.FromSeconds(7));awaittimer.StopAsync(CancellationToken.None);}privatestaticTaskHandleCronTimer(CronEventArgse){if(e.CancellationToken.IsCancellationRequested)returnTask.CompletedTask;returnTask.Run(()=>{Console.WriteLine("Task has completed at {0}.",DateTime.UtcNow);});}}To use CronTimer with dependency injection and logging, follow these steps:
Add the necessary NuGet packages:
dotnet add package Microsoft.Extensions.Logging dotnet add package Microsoft.Extensions.DependencyInjection
Configure the services in your
Startupclass:usingMicrosoft.Extensions.DependencyInjection;usingMicrosoft.Extensions.Logging;publicclassStartup{publicvoidConfigureServices(IServiceCollectionservices){services.AddLogging(configure =>configure.AddConsole());services.AddSingleton<ITimeProvider,SystemTimeProvider>();services.AddSingleton<IDelayProvider,SystemDelayProvider>();services.AddTransient<CronTimer>(provider =>{varlogger=provider.GetRequiredService<ILogger<CronTimer>>();vartimeProvider=provider.GetService<ITimeProvider>();vardelayProvider=provider.GetService<IDelayProvider>();returnnewCronTimer(options =>{options.AddCronTabs(newCronTab("*/5 * * * * *",CronFormats.IncludeSeconds));},timeProvider,delayProvider,logger);});}}
Use the
CronTimerin your application:usingSystem;usingSystem.Threading;usingSystem.Threading.Tasks;usingMicrosoft.Extensions.DependencyInjection;usingMicrosoft.Extensions.Logging;classProgram{staticasyncTaskMain(string[]args){varserviceProvider=newServiceCollection().AddLogging(configure =>configure.AddConsole()).AddSingleton<ITimeProvider,SystemTimeProvider>().AddSingleton<IDelayProvider,SystemDelayProvider>().AddTransient<CronTimer>(provider =>{varlogger=provider.GetRequiredService<ILogger<CronTimer>>();vartimeProvider=provider.GetService<ITimeProvider>();vardelayProvider=provider.GetService<IDelayProvider>();returnnewCronTimer(options =>{options.AddCronTabs(newCronTab("*/5 * * * * *",CronFormats.IncludeSeconds));},timeProvider,delayProvider,logger);}).BuildServiceProvider();vartimer=serviceProvider.GetRequiredService<CronTimer>();timer.TriggeredEventHandler+=async(s,e)=>awaitHandleCronTimer(e);timer.Start();// Let the timer run for 7 secondsawaitTask.Delay(TimeSpan.FromSeconds(7));awaittimer.StopAsync(CancellationToken.None);}privatestaticTaskHandleCronTimer(CronEventArgse){if(e.CancellationToken.IsCancellationRequested)returnTask.CompletedTask;returnTask.Run(()=>{Console.WriteLine("Task has completed at {0}.",DateTime.UtcNow);});}}
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.