Mediaplugin: Only one operation can be active at at time

Created on 17 Mar 2017  路  3Comments  路  Source: jamesmontemagno/MediaPlugin

Hi James,
I have a question for you. I've created a component for an image gallery. A user can take or pick up a picture and I'm using your control. For about one year nobody complain. In the last two weeks a strange problem came up: I'm receive this error:

Only one operation can be active at at time

I think this means a user can take two picture at the same time (?). I invoke

public ICommand CameraCommand {
    get { return _cameraCommand ?? new Command(async () => 
             await ExecuteCameraCommand(), () => CanExecuteCameraCommand()); }
}

public bool CanExecuteCameraCommand() {
    if (!initialized)
        InitMedia();

    if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
    {
        return false;
    }
    return true;
}

public async Task ExecuteCameraCommand() {
    if (!initialized)
    InitMedia();

    var file = await CrossMedia.Current.TakePhotoAsync(
                    new Plugin.Media.Abstractions.StoreCameraMediaOptions() {
            CompressionQuality = 100,
            Directory = path,
            Name = Guid.NewGuid().ToString() + "_original",
    });

    // more code
}

Can I have an advice for you about it? Thank you in advance!

Most helpful comment

@erossini I respond because I have an open issue, too here.

Take a look on my gist: https://gist.github.com/ericbrunner/5a4c76cb5cbabc3a992c5a97a216c81c

Your ExecuteCameraCommand is probably invoked twice at the same time , before the Task returned by CrossMedia.Current.TakePhotoAsync has finished.

In short: Only one operation is allowed at the same time until the task finished.

I use a SemaphoreSlimlock to achieve exactly that. The SemaphoreSlimlock is configured to only allow one async operation at a time. Hope it helps you out.

All 3 comments

@erossini I respond because I have an open issue, too here.

Take a look on my gist: https://gist.github.com/ericbrunner/5a4c76cb5cbabc3a992c5a97a216c81c

Your ExecuteCameraCommand is probably invoked twice at the same time , before the Task returned by CrossMedia.Current.TakePhotoAsync has finished.

In short: Only one operation is allowed at the same time until the task finished.

I use a SemaphoreSlimlock to achieve exactly that. The SemaphoreSlimlock is configured to only allow one async operation at a time. Hope it helps you out.

@ericbrunner Thanks!

@erossini You are welcome. Did it solve your issue?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Glabenwitt picture Glabenwitt  路  4Comments

irinakush picture irinakush  路  6Comments

mdcso picture mdcso  路  4Comments

Brosten picture Brosten  路  3Comments

Philyorkshire picture Philyorkshire  路  7Comments