
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Late4dTrain.CronTimer
Advanced tools
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.
You can install the CronTimer library via NuGet:
dotnet add package Late4dTrain.CronTimer
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); });
}
}
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 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);
});
}
}
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); });
}
}
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.
FAQs
Simple and lightweight library for creating cron jobs in .NET
We found that late4dtrain.crontimer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.