39 lines
921 B
C#
39 lines
921 B
C#
using Microsoft.Extensions.Hosting;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace RMuseum.Services.Implementation
|
|
{
|
|
internal class QueuedHostedService : BackgroundService
|
|
{
|
|
public QueuedHostedService(IBackgroundTaskQueue taskQueue)
|
|
{
|
|
TaskQueue = taskQueue;
|
|
}
|
|
|
|
public IBackgroundTaskQueue TaskQueue { get; }
|
|
|
|
protected async override Task ExecuteAsync(
|
|
CancellationToken cancellationToken)
|
|
{
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
{
|
|
var workItem = await TaskQueue.DequeueAsync(cancellationToken);
|
|
|
|
try
|
|
{
|
|
await workItem(cancellationToken);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|