When we slide down the screen on camera page or when we click on the home, calling OnAppearing() and creating a new task TakePhotoAsync() ,So Camera is not getting opened and throwing an exception saying ”Only one task can be Active at a time".
In OnDisAppearing() how do I cancel this async task?.
Steps to reproduce the Behavior
Open the camera,Slide down the screen or click on the home.and re-open the same camera page .
private async Task<MediaFile> OpenCamera()
{
try
{
if (isFirstCameraLoaded)
return null;
isFirstCameraLoaded = true;
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("No Camera", StringResources.NoCamereAlert, "OK");
return null;
}
FileName = “photo” + DateTime.Now.Ticks.ToString() + ".png";
var mediaOptions = new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Name = FileName;
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Small,
CompressionQuality = 92
};
var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions);
return file;
}
catch (Exception ex)
{
await DisplayAlert("Unable to Open Camera", ex.Message, "OK");
return null;
}
}
protected async override void OnAppearing()
{
base.OnAppearing();
if (ImageCollection.Count == 0)
{
mediaFile = await OpenCamera();
processMedia(mediaFile);
}
}
```
Version Number of Plugin: 3.0.1
Device Tested On:iPhone6
Simulator Tested On:
Version of VS: 2015
Version of Xamarin:6.3(build 864)
Versions of other things you are using:
@Soumya-G Could you share the complete stack trace from the exception?
Hi Prashant,
The Exception says - "Only one operation can be performed at a time". The problem is simple.
We wish to be able to cancel any camera task when the page receives - OnDisappearing Event .
We could not find any method in the API to Cancel the CrossMedia.Current's tasks.
We could write our code to cancel our Task. However, cancelling our own task does not cancel the CrossMedia.Current instance's Tasks.
Please suggest
@Soumya-G Could you share the complete stack trace from the exception?
A sample project demonstrating the problem will help us to accelerate identifying the fix
Add a semiphore into your app to not launch until the ask has completed, or save the task around.
@jamesmontemagno would you have a complete working example for this? You've made it sound simple but it definitely isn't. In fact, built-in API for it would be more than welcome.