Skip to content

Instantly share code, notes, and snippets.

@seesharprun
Created October 18, 2024 22:02
Show Gist options
  • Save seesharprun/c9d0dd972f217998cc9862f7732c8bdc to your computer and use it in GitHub Desktop.
Save seesharprun/c9d0dd972f217998cc9862f7732c8bdc to your computer and use it in GitHub Desktop.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
</ItemGroup>
</Project>
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<PingWorker>();
var host = builder.Build();
await host.RunAsync();
public class PingWorker(ILogger<PingWorker> logger) : BackgroundService
{
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
Timer timer = new (
callback: (_) => logger.LogInformation($"This was generated at {DateTime.Now:R}"),
state: null,
dueTime: TimeSpan.Zero,
period: TimeSpan.FromSeconds(5)
);
return Task.CompletedTask;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment