Created
October 18, 2024 22:02
-
-
Save seesharprun/c9d0dd972f217998cc9862f7732c8bdc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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