Mediaplugin: How do I cancel CrossMedia.Current.TakePhotoAsync() Task ?

Created on 25 Sep 2017  ·  5Comments  ·  Source: jamesmontemagno/MediaPlugin

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 .

Code snippet

    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:

need-more-info

All 5 comments

@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.

  1. When we load the page, we trigger the TakePhotoAsync (OnAppearing) due to business requirement.
  2. However, in between, if another overlay comes, like InComing call, or the user swipes down to see the notifications, or settings at that point, Our camera page receives - OnDisappearing - Event
  3. When the user returns to app, the camera page again receives on Appearing, which in turn triggers our TakePhotoAsync call.
    At this point, another thread is already active and our page is waiting for the Camera Task response.
    We wish to be able to cancel the current CrossMedia.Current.TakePhotoAsync call when the page receive OnDisappearing call. So when it reappears, we can trigger a new Task again. without waiting for the previous task to complete and hence avoiding two simultaneous calls causing the exception.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Julien-Mialon picture Julien-Mialon  ·  7Comments

jamesmontemagno picture jamesmontemagno  ·  4Comments

Greg-Freeman picture Greg-Freeman  ·  7Comments

mdcso picture mdcso  ·  4Comments

erossini picture erossini  ·  3Comments