-
Notifications
You must be signed in to change notification settings - Fork 492
Fix connect camera cancellation #3172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
853ccb5
1949212
12abe0b
05044c3
054921a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,9 @@ namespace CommunityToolkit.Maui.Core.Handlers; | |
| /// </summary> | ||
| public partial class CameraViewHandler : ViewHandler<ICameraView, NativePlatformCameraPreviewView>, IDisposable | ||
| { | ||
| CancellationTokenSource? cts; | ||
| bool isDisposed; | ||
|
|
||
| /// <summary> | ||
| /// The currently defined mappings between properties on the <see cref="ICameraView"/> and | ||
| /// properties on the <see cref="NativePlatformCameraPreviewView"/>. | ||
|
|
@@ -82,9 +85,29 @@ void Init(ICameraView view) | |
| /// <inheritdoc/> | ||
| protected override async void ConnectHandler(NativePlatformCameraPreviewView platformView) | ||
| { | ||
| base.ConnectHandler(platformView); | ||
|
|
||
| await CameraManager.ConnectCamera(CancellationToken.None); | ||
| cts?.Cancel(); | ||
| using var newCts = cts = new(); | ||
| try | ||
| { | ||
| base.ConnectHandler(platformView); | ||
| await CameraManager.ConnectCamera(newCts.Token); | ||
| } | ||
| catch (OperationCanceledException) when (newCts.IsCancellationRequested) | ||
| { | ||
| // Catch and ignore the OperationCanceledException if it was caused by our own cancellation token, | ||
| // e.g. the handler is disconnected before the camera connection process completes. | ||
| } | ||
| catch (Exception) | ||
| { | ||
| DisconnectHandler(platformView); | ||
| } | ||
|
Comment on lines
+100
to
+103
|
||
| finally | ||
| { | ||
| if (cts == newCts) | ||
| { | ||
| cts = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
|
|
@@ -103,11 +126,20 @@ protected override void DisconnectHandler(NativePlatformCameraPreviewView platfo | |
| /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> | ||
| protected virtual void Dispose(bool disposing) | ||
| { | ||
| if (isDisposed) | ||
| { | ||
| return; | ||
| } | ||
| if (disposing) | ||
| { | ||
| cts?.Cancel(); | ||
| cts = null; | ||
|
|
||
| cameraManager?.Dispose(); | ||
| cameraManager = null; | ||
| } | ||
|
|
||
| isDisposed = true; | ||
| } | ||
|
|
||
| #if WINDOWS | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.