From 4b7835c62f945dd810d3d1a8be9028391be8bbaf Mon Sep 17 00:00:00 2001 From: Kirill Krylov Date: Fri, 24 Nov 2023 11:56:59 +0200 Subject: [PATCH] Added Cancellation token in ListenCommand Signed-off-by: Kirill Krylov --- clio/Command/ListenCommand.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/clio/Command/ListenCommand.cs b/clio/Command/ListenCommand.cs index c9d0a49d..6043d071 100644 --- a/clio/Command/ListenCommand.cs +++ b/clio/Command/ListenCommand.cs @@ -46,6 +46,8 @@ public class ListenCommand : Command private const string StopLogBroadcast = "/rest/ATFLogService/ResetConfiguration"; private string LogFilePath = string.Empty; private bool Silent; + private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); + #region Constructors: Public public ListenCommand(IApplicationClient applicationClient,ILogger logger,EnvironmentSettings environmentSettings, IFileSystem fileSystem){ @@ -61,11 +63,13 @@ public ListenCommand(IApplicationClient applicationClient,ILogger logger,Environ #region Methods: Public public override int Execute(ListenOptions options){ + CancellationToken token = _cancellationTokenSource.Token; LogFilePath = options.FileName; Silent = options.Silent; - _applicationClient.Listen(CancellationToken.None); + _applicationClient.Listen(token); StartLogger(options); Console.ReadKey(); + _cancellationTokenSource.Cancel(); StopLogger(); return 0; } @@ -81,13 +85,13 @@ private void StartLogger(ListenOptions options){ JsonSerializerOptions serializerOptions = new (){PropertyNamingPolicy = JsonNamingPolicy.CamelCase}; string payloadString = JsonSerializer.Serialize(payload,serializerOptions); _applicationClient.ExecutePostRequest(requestUrl,payloadString); - } private void StopLogger(){ string rootPath = _environmentSettings.IsNetCore ? _environmentSettings.Uri : _environmentSettings.Uri + @"/0"; string requestUrl = rootPath+StopLogBroadcast; _applicationClient.ExecutePostRequest(requestUrl,string.Empty); + } private void OnMessageReceived(object sender, WsMessage message){