IsCancellationRequested
This commit is contained in:
parent
21ca438183
commit
84c5355dcf
@ -128,7 +128,7 @@ internal static class HelperPhysicalAddress
|
||||
return keyValuePairs;
|
||||
}
|
||||
|
||||
internal static bool ParsePackets(AppSettings appSettings, ILogger<Worker> logger)
|
||||
internal static bool ParsePackets(AppSettings appSettings, ILogger<Worker> logger, CancellationToken stoppingToken)
|
||||
{
|
||||
ILiveDevice? liveDevice = null;
|
||||
Version version = Pcap.SharpPcapVersion;
|
||||
@ -162,7 +162,7 @@ internal static class HelperPhysicalAddress
|
||||
logger.LogInformation("");
|
||||
liveDevice.Open(DeviceModes.Promiscuous, appSettings.PhysicalAddressConfiguration.ReadTimeoutMilliseconds);
|
||||
logger.LogInformation("-- Listening on {Name} {Description}", liveDevice.Name, liveDevice.Description);
|
||||
while (true)
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
status = liveDevice.GetNextPacket(out PacketCapture e);
|
||||
if (status != GetPacketStatus.PacketRead)
|
||||
|
19
Worker.cs
19
Worker.cs
@ -22,24 +22,19 @@ public partial class Worker : BackgroundService
|
||||
_IsWindowsService = collection.Contains(nameof(WindowsServiceLifetime));
|
||||
}
|
||||
|
||||
private void Body()
|
||||
{
|
||||
_Logger.LogInformation("A) Next execute will be at {date}", DateTime.Now.AddMilliseconds(_AppSettings.MillisecondsDelay).ToString("yyyy-MM-dd hh:mm:ss.fff tt"));
|
||||
_ = _AppSettings.Helper switch
|
||||
{
|
||||
nameof(Helpers.HelperPhysicalAddress) => Helpers.HelperPhysicalAddress.ParsePackets(_AppSettings, _Logger),
|
||||
_ => throw new NotSupportedException()
|
||||
};
|
||||
_Logger.LogInformation("B) Next execute will be at {date}", DateTime.Now.AddMilliseconds(_AppSettings.MillisecondsDelay).ToString("yyyy-MM-dd hh:mm:ss.fff tt"));
|
||||
}
|
||||
|
||||
private async Task Body(CancellationToken stoppingToken)
|
||||
{
|
||||
if (!_IsWindowsService)
|
||||
throw new EvaluateException("Set break point and skip!");
|
||||
while (_IsWindowsService && !stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
Body();
|
||||
_Logger.LogInformation("A) Next execute will be at {date}", DateTime.Now.AddMilliseconds(_AppSettings.MillisecondsDelay).ToString("yyyy-MM-dd hh:mm:ss.fff tt"));
|
||||
_ = _AppSettings.Helper switch
|
||||
{
|
||||
nameof(Helpers.HelperPhysicalAddress) => Helpers.HelperPhysicalAddress.ParsePackets(_AppSettings, _Logger, stoppingToken),
|
||||
_ => throw new NotSupportedException()
|
||||
};
|
||||
_Logger.LogInformation("B) Next execute will be at {date}", DateTime.Now.AddMilliseconds(_AppSettings.MillisecondsDelay).ToString("yyyy-MM-dd hh:mm:ss.fff tt"));
|
||||
await Task.Delay(_AppSettings.MillisecondsDelay, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user