fix: fix memory leak

This commit is contained in:
Иванов Иван 2024-08-08 21:13:54 +02:00
parent e95d748f00
commit ee0b8db572

View File

@ -28,6 +28,7 @@ namespace Client.Infrastructure.Service
private CancellationTokenSource? cancellationTokenSource; private CancellationTokenSource? cancellationTokenSource;
public ObservableCollection<PathSegment> Path { get; private set; } = new ObservableCollection<PathSegment>(); public ObservableCollection<PathSegment> Path { get; private set; } = new ObservableCollection<PathSegment>();
public bool IsBusy { get; private set; } = false;
public async Task MoveUntilReachedAsync(Vector3 location) public async Task MoveUntilReachedAsync(Vector3 location)
{ {
@ -40,6 +41,8 @@ namespace Client.Infrastructure.Service
public async Task<bool> MoveAsync(Vector3 location) public async Task<bool> MoveAsync(Vector3 location)
{ {
IsBusy = true;
if (cancellationTokenSource != null) if (cancellationTokenSource != null)
{ {
cancellationTokenSource.Cancel(); cancellationTokenSource.Cancel();
@ -50,7 +53,7 @@ namespace Client.Infrastructure.Service
try try
{ {
return await Task.Run(() => return await Task.Run(async () =>
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
@ -63,18 +66,22 @@ namespace Client.Infrastructure.Service
{ {
worldHandler.RequestMoveToLocation(node.To); worldHandler.RequestMoveToLocation(node.To);
if (!WaitForNodeReaching(cancellationToken, node)) var reached = await WaitForNodeReaching(cancellationToken, node);
if (!reached)
{ {
IsBusy = false;
return false; return false;
} }
Path.Remove(node); Path.Remove(node);
} }
IsBusy = false;
return true; return true;
}, cancellationToken); }, cancellationToken);
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
IsBusy = false;
return true; return true;
} }
} }
@ -106,7 +113,7 @@ namespace Client.Infrastructure.Service
} }
} }
private bool WaitForNodeReaching(CancellationToken token, PathSegment node) private async Task<bool> WaitForNodeReaching(CancellationToken token, PathSegment node)
{ {
var hero = worldHandler.Hero; var hero = worldHandler.Hero;
@ -131,7 +138,7 @@ namespace Client.Infrastructure.Service
return false; return false;
} }
} }
Task.Delay(25); await Task.Delay(25);
} }
return true; return true;