Camera is not always correctly detected

Luca Franceschini 1 Reputation point
2020-11-16T11:40:05.917+00:00

Sometimes (this is not always reproducible) the following call gives me an empty list, failing to detect a USB connected camera:

await MediaFrameSourceGroup.FindAllAsync().AsTask().ConfigureAwait(false);  

The camera is correctly connected though, since other applications are able to use it. Disconnecting and reconnecting the camera seems to fix it, but this happens quite often. The following code however successfully detects the camera (but I need a source group for video capture purposes):

await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture).AsTask().ConfigureAwait(false);  

The issue appeared on more than one computer.

Is there anything I should consider when retrieving a camera this way? Is there a more robust way to do this?

I followed this guide to process frames: https://learn.microsoft.com/en-us/windows/uwp/audio-video-camera/process-media-frames-with-mediaframereader

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,663 questions
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,616 Reputation points
    2020-11-17T07:52:22.39+00:00

    When you call the method from the UI, deadlock is generally not caused by this. The ConfigureAwait method isn’t special, it’s not recognized in any special way by the compiler or by the runtime. It is simply a method that returns a struct (a ConfiguredTaskAwaitable) that wraps the original task it was called on as well as the specified Boolean value. When you use ConfigureAwait(false) , even if there is a current context or scheduler to call back to, it pretends as if there isn’t. So I think it's no need for you to use ConfigureAwait(false). For more details about ConfigureAwait , please refer to the Stephen's blog ConfigureAwait FAQ

    By the way, I tested below code in my .Net core 5.0 project ,and both of them can detect the camera without disconnecting and reconnecting the camera.

     var frameSourceGroups0 = await MediaFrameSourceGroup.FindAllAsync().AsTask().ConfigureAwait(false);  
     var frameSourceGroups1 = await MediaFrameSourceGroup.FindAllAsync();  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.