IsCancellationRequested

This commit is contained in:
Mike Phares 2024-02-03 18:37:43 -07:00
parent 21ca438183
commit 84c5355dcf
2 changed files with 9 additions and 14 deletions

View File

@ -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)

View File

@ -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);
}
}