/// <summary> /// Get initialized instance of the Kinect device. /// </summary> /// /// <param name="deviceID">ID of the Kinect device to get instance of, [0, <see cref="DeviceCount"/>),</param> /// /// <returns>Returns initialized Kinect device. Use <see cref="Dispose()"/> method /// when the device is no longer required.</returns> /// /// <exception cref="ArgumentException">There is no Kinect device with specified ID connected to the system.</exception> /// <exception cref="ConnectionFailedException">Failed connecting to the Kinect device specified ID.</exception> /// public static Kinect GetDevice(int deviceID) { if ((deviceID < 0) || (deviceID >= DeviceCount)) { throw new ArgumentException("There is no Kinect device with specified ID connected to the system."); } bool needToStartStatusThread = false; Kinect kinect = null; lock (openDevices) { needToStartStatusThread = (openDevices.Count == 0); // check if the device is open already if (!openDevices.ContainsKey(deviceID)) { IntPtr devicePointer = IntPtr.Zero; // connect to Kinect device witht the specified ID if (KinectNative.freenect_open_device(KinectNative.Context, ref devicePointer, deviceID) != 0) { throw new ConnectionFailedException("Failed connecting to the Kinect device with ID: " + deviceID); } openDevices.Add(deviceID, new DeviceContext(devicePointer)); } openDevices[deviceID].ReferenceCounter++; kinect = new Kinect(openDevices[deviceID].Device, deviceID); } if (needToStartStatusThread) { StartStatusThread(); } return(kinect); }