Home - mng token - Content

Background tasks with hosted services in ASP.NET Core

public class MonitorLoop{ private readonly IBackgroundTaskQueue _taskQueue; private readonly ILogger _logger; private readonly CancellationToken _cancellationToken; public MonitorLoop(IBackgroundTaskQueue taskQueue, ILogger<MonitorLoop> logger, IHostApplicationLifetime applicationLifetime) { _taskQueue = taskQueue; _logger = logger; _cancellationToken = applicationLifetime.ApplicationStopping; } public void StartMonitorLoop() { _logger.LogInformation("MonitorAsync Loop is starting."); // Run a console user input loop in a background thread Task.Run(async () => await MonitorAsync()); } private async ValueTask MonitorAsync() { while (!_cancellationToken.IsCancellationRequested) { var keyStroke = Console.ReadKey(); if (keyStroke.Key == ConsoleKey.W) { // Enqueue a background work item await _taskQueue.QueueBackgroundWorkItemAsync(BuildWorkItem); } } } private async ValueTask BuildWorkItem(CancellationToken token) { // Simulate three 5-second tasks to complete // for each enqueued work item int delayLoop = 0; var guid = Guid.NewGuid().ToString(); _logger.LogInformation("Queued Background Task {Guid} is starting.", guid); while (!token.IsCancellationRequested && delayLoop < 3) { try { await Task.Delay(TimeSpan.FromSeconds(5), token); } catch (OperationCanceledException) { // Prevent throwing if the Delay is cancelled } delayLoop++; _logger.LogInformation("Queued Background Task {Guid} is running. " + "{DelayLoop}/3", guid, delayLoop); } if (delayLoop == 3) { _logger.LogInformation("Queued Background Task {Guid} is complete.", guid); } else { _logger.LogInformation("Queued Background Task {Guid} was cancelled.", guid); } }}

Marvelous NFTs (Bad Days)

MNFT Price Live Data The live Marvelous NFTs (Bad Days) price today is $0.057328 USD with a 24-hour

Market Cap

General Info Careers Methodology Liquidity Book Press Kit Widgets Changelog Privacy Policy COIN360 W

Mixed Wholesale & Job Lots

public class MonitorLoop{ private readonly IBackgroundTaskQueue _taskQueue; private readonly ILogger