await GetKinect in FluentKinect

Today's project by Marcus Kohnert brings us back to the FluentKinect and Marcus' recent update to make it awaitable...

await GetKinect()

In the first blog post about FluentKinect I’ve mentioned that I’m not very happy with the actual process of getting a KinectSensor instance from the KinectSensors collection.

FluentKinect has now been updated and the KinectConnector’s static method GetKinect is now awaitable.

var kinect = await KinectConnector.GetKinect();

kinect.Start();

It’s not thread safe at the moment but there are a few improvements now in my opinion.

    • You can start and debug your actual program without a Kinect controller connected because no more exception is thrown. This helps with Kinect programming on a plane for example. <img src='http://video.ch9.ms/ecn/content/images/emoticons/emotion-5.gif?v=c9' alt='Wink' />
    • Your program starts faster because GetKinect returns immediately
    • Since GetKinect returns a Task you get the ability to await the result

Project Information URL: http://passiondev.wordpress.com/2013/07/30/await-getkinect/

Project Download URL: http://www.nuget.org/packages/FluentKinect

Project Source URL: https://github.com/MarcusKohnert/FluentKinect

public static Task<KinectSensor> GetKinect()
{
    return Task.Factory.StartNew<KinectSensor>(() =>
    {
        if (kinectSensor != null) return kinectSensor;

        var kinect = KinectSensor.KinectSensors
                            .FirstOrDefault(_ => _.Status == KinectStatus.Connected);
        if (kinect != null)
        {
            kinectSensor = kinect;
            return kinectSensor;
        }

        using (var signal = new ManualResetEventSlim())
        {
            KinectSensor.KinectSensors.StatusChanged += (s, e) =>
            {
                if (e.Status == KinectStatus.Connected)
                {
                    kinectSensor = e.Sensor;
                    coordinateMapper = new CoordinateMapper(kinectSensor);
                    signal.Set();
                }
            };

            signal.Wait();
        }

        return kinectSensor;
    });
}

Contact Information:

Tags:

Follow the Discussion

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.