divan/RMuseum/Services/Implementation/QueuedHostedService.cs
Hamid Reza Mohammadi 5991d7fa5f Add project files.
2020-08-14 21:06:22 +04:30

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
{
}
}
}
}
}