make cython app exit correctly

This commit is contained in:
Alex Bezdieniezhnykh
2025-02-11 20:40:49 +02:00
parent 9973a16ada
commit 43cae0d03c
9 changed files with 87 additions and 65 deletions
+21 -19
View File
@@ -14,6 +14,7 @@ using Azaion.Common.DTO.Config;
using Azaion.Common.Events;
using Azaion.Common.Extensions;
using Azaion.Common.Services;
using Azaion.CommonSecurity.Services;
using LibVLCSharp.Shared;
using MediatR;
using Microsoft.WindowsAPICodePack.Dialogs;
@@ -40,6 +41,7 @@ public partial class Annotator
private readonly AnnotationService _annotationService;
private readonly IDbFactory _dbFactory;
private readonly IInferenceService _inferenceService;
private readonly IResourceLoader _resourceLoader;
private ObservableCollection<DetectionClass> AnnotationClasses { get; set; } = new();
private bool _suspendLayout;
@@ -69,7 +71,8 @@ public partial class Annotator
ILogger<Annotator> logger,
AnnotationService annotationService,
IDbFactory dbFactory,
IInferenceService inferenceService)
IInferenceService inferenceService,
IResourceLoader resourceLoader)
{
InitializeComponent();
_appConfig = appConfig.Value;
@@ -83,6 +86,7 @@ public partial class Annotator
_annotationService = annotationService;
_dbFactory = dbFactory;
_inferenceService = inferenceService;
_resourceLoader = resourceLoader;
Loaded += OnLoaded;
Closed += OnFormClosed;
@@ -397,6 +401,7 @@ public partial class Annotator
private void OnFormClosed(object? sender, EventArgs e)
{
_resourceLoader.StopPython();
MainCancellationSource.Cancel();
DetectionCancellationSource.Cancel();
_mediaPlayer.Stop();
@@ -494,20 +499,6 @@ public partial class Annotator
private (TimeSpan Time, List<Detection> Detections)? _previousDetection;
private List<string> GetLvFiles()
{
return Dispatcher.Invoke(() =>
{
var source = LvFiles.ItemsSource as IEnumerable<MediaFileInfo>;
var items = source?.Skip(LvFiles.SelectedIndex)
.Take(Constants.DETECTION_BATCH_SIZE)
.Select(x => x.Path)
.ToList();
return items ?? new List<string>();
});
}
public void AutoDetect(object sender, RoutedEventArgs e)
{
if (IsInferenceNow)
@@ -529,18 +520,29 @@ public partial class Annotator
var ct = DetectionCancellationSource.Token;
_ = Task.Run(async () =>
{
var files = GetLvFiles();
while (files.Any() && !ct.IsCancellationRequested)
while (!ct.IsCancellationRequested)
{
var files = new List<string>();
await Dispatcher.Invoke(async () =>
{
files = (LvFiles.ItemsSource as IEnumerable<MediaFileInfo>)?.Skip(LvFiles.SelectedIndex)
.Take(Constants.DETECTION_BATCH_SIZE)
.Select(x => x.Path)
.ToList();
await _mediator.Publish(new AnnotatorControlEvent(PlaybackControlEnum.Play), ct);
await ReloadAnnotations();
});
await _inferenceService.RunInference(files, async annotationImage => await ProcessDetection(annotationImage), ct);
files = GetLvFiles();
Dispatcher.Invoke(() => LvFiles.Items.Refresh());
Dispatcher.Invoke(() =>
{
if (LvFiles.SelectedIndex + files.Count >= LvFiles.Items.Count)
DetectionCancellationSource.Cancel();
LvFiles.SelectedIndex += files.Count;
LvFiles.Items.Refresh();
});
}
Dispatcher.Invoke(() =>
{