In writing quite a bit of the code in these posts;
I’d sometimes been trying to take care such that if I had open readers for data ( like body data, infrared data, etc. ) from a KinectSensor and that sensor suddenly became unavailable (e.g. someone unplugged it) then I’d try to make sure I closed the readers and re-opened them when the sensor became available again.
I’d had a nagging feeling all along though that I might be wasting my time and today I thought I’d test that out and this little piece of console code;
namespace ConsoleApplication1 { using Microsoft.Kinect; using System; class Program { static void Main(string[] args) { bool readerOpen = false; KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); EventHandler<IsAvailableChangedEventArgs> handler = (s,e) => { if (!readerOpen && sensor.IsAvailable) { readerOpen = true; var reader = sensor.ColorFrameSource.OpenReader(); reader.FrameArrived += (a,b) => { Console.WriteLine("frame"); }; } }; sensor.IsAvailableChanged += handler; handler(null, null); Console.ReadLine(); } } }
seems to suggest that my nagging feeling was right. This code picks up frames from the Kinect sensor whether;
- The sensor is plugged in when the code first runs.
- The sensor is not plugged in when the code first runs.
- The sensor is connected/disconnected while the code is running.
so – as far as I can tell once the code is up and running with an open reader, the underlying bits do the right thing to check whether the sensor has become available/unavailable and to continue to deliver data on that reader whenever they can.
I then wondered whether I could take it even further. What if I attempt to open a reader even if the sensor isn’t available;
namespace ConsoleApplication1 { using Microsoft.Kinect; using System; class Program { static void Main(string[] args) { KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); var reader = sensor.BodyFrameSource.OpenReader(); reader.FrameArrived += (s, e) => { Console.WriteLine("frame"); }; Console.ReadLine(); } } }
and, again, this code works fine whether the sensor is initially available, whether it becomes available later on and whether it goes available/unavailable while running.
That’s a lot of simplification – if only I’d know that before