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!
@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?
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
ExecuteCameraCommandis probably invoked twice at the same time , before the Task returned byCrossMedia.Current.TakePhotoAsynchas finished.In short: Only one operation is allowed at the same time until the task finished.
I use a
SemaphoreSlimlock to achieve exactly that. TheSemaphoreSlimlock is configured to only allow one async operation at a time. Hope it helps you out.