SystemBackgroudService.cs
1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using HHECS.DAQServer.Models;
namespace HHECS.DAQServer.Services
{
public class SystemBackgroundService : BackgroundService
{
private readonly IFreeSql _freeSql;
private readonly DataCacheService _dataCacheService;
private readonly ILogger<SystemBackgroundService> _logger;
private readonly int _limit = 1000;
public SystemBackgroundService(IFreeSql freeSql, DataCacheService dataCacheService, ILogger<SystemBackgroundService> logger)
{
_freeSql = freeSql;
_dataCacheService = dataCacheService;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var temps = new List<EquipmentDataRecord>();
while (!stoppingToken.IsCancellationRequested)
{
try
{
if (temps.Count < _limit)
{
for (int i = 0; i < _limit; i++)
{
if (_dataCacheService.EquipmentDataRecordQueue.IsEmpty)
{
break;
}
_dataCacheService.EquipmentDataRecordQueue.TryDequeue(out var data);
temps.Add(data);
}
}
if (temps.Count > 0)
{
_freeSql.Insert(temps).ExecuteAffrows();
temps.Clear();
}
await Task.Delay(1000, stoppingToken);
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
}
}
}
}