VideoDeviceController.TryAcquireExclusiveControl Method

Definition

Requests exclusive control of the camera with the specified device ID.

public:
 virtual bool TryAcquireExclusiveControl(Platform::String ^ deviceId, MediaCaptureDeviceExclusiveControlReleaseMode mode) = TryAcquireExclusiveControl;
bool TryAcquireExclusiveControl(winrt::hstring const& deviceId, MediaCaptureDeviceExclusiveControlReleaseMode const& mode);
public bool TryAcquireExclusiveControl(string deviceId, MediaCaptureDeviceExclusiveControlReleaseMode mode);
function tryAcquireExclusiveControl(deviceId, mode)
Public Function TryAcquireExclusiveControl (deviceId As String, mode As MediaCaptureDeviceExclusiveControlReleaseMode) As Boolean

Parameters

deviceId
String

Platform::String

winrt::hstring

The device ID of the camera for which exclusive control is requested. The device ID can be obtained with the DeviceInformation class.

mode
MediaCaptureDeviceExclusiveControlReleaseMode

A value from the MediaCaptureDeviceExclusiveControlReleaseMode enumeration specifying the conditions under which exclusive control is released.

Returns

Boolean

bool

True if exclusive control of the camera was acquired; otherwise, false.

Windows requirements

Device family
Windows 11 Insider Preview (introduced in 10.0.23504.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v15.0)

Examples

This example demonstrates how an application using camera in exclusive control can ensure that camera configuration is set before start of capture and will not be altered by another application that has access to this camera by acquiring the exclusive control lock ahead of it.

private static System.Threading.ManualResetEvent _exclusiveLockAcquire = new System.Threading.ManualResetEvent(false);

public static void RecordVideo()
{
    MediaCapture mediacapture = new MediaCapture();
    await mediacapture.InitializeAsync();

    mediacapture.CaptureDeviceExclusiveControlStatusChanged += 
Mediacapture_CaptureDeviceExclusiveControlStatusChanged;

    _exclusiveLockAcquire.WaitOne();
    _exclusiveLockAcquire.Reset();

    // configure camera - blocking other application from changing the configuration.

    // record video
}

private static void Mediacapture_CaptureDeviceExclusiveControlStatusChanged(MediaCapture sender, MediaCaptureDeviceExclusiveControlStatusChangedEventArgs args)
{
    if (args.Status == MediaCaptureDeviceExclusiveControlStatus.ExclusiveControlAvailable)
    {
        if (sender.VideoDeviceController().TryAcquireExclusiveControl(
            args.DeviceId(),     
            MediaCaptureDeviceExclusiveControlReleaseMode.OnAllStreamsStopped))
        {
            _exclusiveLockAcquire.Set();
        }
    }
}

Applies to