|
31 | 31 | using static Hi3Helper.Logger; |
32 | 32 | // ReSharper disable PartialTypeWithSinglePart |
33 | 33 | // ReSharper disable StringLiteralTypo |
| 34 | +// ReSharper disable AsyncVoidMethod |
34 | 35 |
|
35 | 36 | #nullable enable |
36 | 37 | namespace CollapseLauncher.Helper.Background.Loaders |
@@ -71,6 +72,7 @@ private static bool IsUseVideoBgDynamicColorUpdate |
71 | 72 | private readonly MediaPlayerElement? _currentMediaPlayerFrame; |
72 | 73 | private readonly Grid _currentMediaPlayerFrameParentGrid; |
73 | 74 | private readonly ImageUI _currentImage; |
| 75 | + private readonly Lock _frameGrabberEventLock = new(); |
74 | 76 |
|
75 | 77 | internal MediaPlayerLoader( |
76 | 78 | FrameworkElement parentUI, |
@@ -325,37 +327,42 @@ await ColorPaletteUtility.ApplyAccentColor(ParentUI, |
325 | 327 | private static async Task<StorageFile> GetFileAsStorageFile(string filePath) |
326 | 328 | => await StorageFile.GetFileFromPathAsync(filePath); |
327 | 329 |
|
328 | | - private void FrameGrabberEvent(MediaPlayer mediaPlayer, object args) |
| 330 | +#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously |
| 331 | + private async void FrameGrabberEvent(MediaPlayer mediaPlayer, object args) |
| 332 | +#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously |
329 | 333 | { |
330 | 334 | if (_isCanvasCurrentlyDrawing == 1) |
331 | 335 | { |
332 | 336 | return; |
333 | 337 | } |
334 | 338 |
|
335 | | - CanvasDrawingSession? drawingSession = null; |
336 | | - try |
337 | | - { |
338 | | - Interlocked.Exchange(ref _isCanvasCurrentlyDrawing, 1); |
339 | | - mediaPlayer.CopyFrameToVideoSurface(_currentCanvasBitmap); |
340 | | - drawingSession = _currentCanvasVirtualImageSource?.CreateDrawingSession(_currentDefaultColor, _currentCanvasDrawArea); |
341 | | - drawingSession?.DrawImage(_currentCanvasBitmap); |
342 | | - _currentCanvasVirtualImageSource?.SuspendDrawingSession(drawingSession); |
343 | | - } |
344 | | - catch |
345 | | - #if DEBUG |
346 | | - (Exception e) |
347 | | - { |
348 | | - LogWriteLine($"[FrameGrabberEvent] Error while drawing frame to bitmap.\r\n{e}", LogType.Warning, true); |
349 | | - } |
350 | | - #else |
351 | | - { |
352 | | - // ignored |
353 | | - } |
354 | | - #endif |
355 | | - finally |
| 339 | + lock (_frameGrabberEventLock) |
356 | 340 | { |
357 | | - CurrentDispatcherQueue.TryEnqueue(() => drawingSession?.Dispose()); |
358 | | - Interlocked.Exchange(ref _isCanvasCurrentlyDrawing, 0); |
| 341 | + _isCanvasCurrentlyDrawing = 1; |
| 342 | + CanvasDrawingSession? drawingSession = null; |
| 343 | + |
| 344 | + try |
| 345 | + { |
| 346 | + mediaPlayer.CopyFrameToVideoSurface(_currentCanvasBitmap); |
| 347 | + drawingSession = _currentCanvasVirtualImageSource?.CreateDrawingSession(_currentDefaultColor, _currentCanvasDrawArea); |
| 348 | + drawingSession?.DrawImage(_currentCanvasBitmap); |
| 349 | + } |
| 350 | + catch |
| 351 | + #if DEBUG |
| 352 | + (Exception e) |
| 353 | + { |
| 354 | + LogWriteLine($"[FrameGrabberEvent] Error while drawing frame to bitmap.\r\n{e}", LogType.Warning, true); |
| 355 | + } |
| 356 | + #else |
| 357 | + { |
| 358 | + // ignored |
| 359 | + } |
| 360 | + #endif |
| 361 | + finally |
| 362 | + { |
| 363 | + CurrentDispatcherQueue.TryEnqueue(() => drawingSession?.Dispose()); |
| 364 | + _isCanvasCurrentlyDrawing = 0; |
| 365 | + } |
359 | 366 | } |
360 | 367 | } |
361 | 368 |
|
|
0 commit comments