From ee0b8db572a17de7f12d2a4940599d616f3865f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=98=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD?= Date: Thu, 8 Aug 2024 21:13:54 +0200 Subject: [PATCH] fix: fix memory leak --- Client/Infrastructure/Service/AsyncPathMover.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Client/Infrastructure/Service/AsyncPathMover.cs b/Client/Infrastructure/Service/AsyncPathMover.cs index c5b34f9..2898591 100644 --- a/Client/Infrastructure/Service/AsyncPathMover.cs +++ b/Client/Infrastructure/Service/AsyncPathMover.cs @@ -28,6 +28,7 @@ namespace Client.Infrastructure.Service private CancellationTokenSource? cancellationTokenSource; public ObservableCollection Path { get; private set; } = new ObservableCollection(); + public bool IsBusy { get; private set; } = false; public async Task MoveUntilReachedAsync(Vector3 location) { @@ -40,6 +41,8 @@ namespace Client.Infrastructure.Service public async Task MoveAsync(Vector3 location) { + IsBusy = true; + if (cancellationTokenSource != null) { cancellationTokenSource.Cancel(); @@ -50,7 +53,7 @@ namespace Client.Infrastructure.Service try { - return await Task.Run(() => + return await Task.Run(async () => { cancellationToken.ThrowIfCancellationRequested(); @@ -63,18 +66,22 @@ namespace Client.Infrastructure.Service { worldHandler.RequestMoveToLocation(node.To); - if (!WaitForNodeReaching(cancellationToken, node)) + var reached = await WaitForNodeReaching(cancellationToken, node); + if (!reached) { + IsBusy = false; return false; } Path.Remove(node); } + IsBusy = false; return true; }, cancellationToken); } catch (OperationCanceledException) { + IsBusy = false; return true; } } @@ -106,7 +113,7 @@ namespace Client.Infrastructure.Service } } - private bool WaitForNodeReaching(CancellationToken token, PathSegment node) + private async Task WaitForNodeReaching(CancellationToken token, PathSegment node) { var hero = worldHandler.Hero; @@ -131,7 +138,7 @@ namespace Client.Infrastructure.Service return false; } } - Task.Delay(25); + await Task.Delay(25); } return true;