Skip to content

Commit 2fc55bc

Browse files
committed
Nested using for bodyFrame
1 parent b64b65c commit 2fc55bc

File tree

1 file changed

+20
-51
lines changed

1 file changed

+20
-51
lines changed

plugin_KinectOne/KinectOne.cs

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,8 @@ public void Initialize()
104104
{
105105
try
106106
{
107-
try
108-
{
109-
if(InitKinect()) InitializeSkeleton(); // Init?
110-
ShutdownInternal(false); // Shut down
111-
}
112-
catch (Exception)
113-
{
114-
// ignored
115-
}
116-
117107
IsInitialized = InitKinect();
118108
Host.Log($"Tried to initialize the Kinect sensor with status: {DeviceStatusString}");
119-
120-
// Try to start the stream
121-
InitializeSkeleton();
122109
}
123110
catch (Exception e)
124111
{
@@ -158,10 +145,18 @@ private void ShutdownInternal(bool unInitialize = true)
158145

159146
private bool InitKinect()
160147
{
148+
// One sensor is currently supported
161149
if ((KinectSensor = KinectSensor.GetDefault()) is null) return false;
162150

151+
// Open the reader for the body frames
152+
BodyFrameReader = KinectSensor.BodyFrameSource.OpenReader();
153+
163154
try
164155
{
156+
// Unregister sensor events
157+
KinectSensor.IsAvailableChanged -= StatusChangedHandler;
158+
BodyFrameReader.FrameArrived -= OnBodyFrameArrivedHandler;
159+
165160
// Try to open the kinect sensor
166161
KinectSensor.Open(); // Open 1st
167162
for (var i = 0; i < 20; i++)
@@ -174,25 +169,9 @@ private bool InitKinect()
174169
// Get connected get connected
175170
Thread.Sleep(1000);
176171

177-
try
178-
{
179-
// Try to open the kinect sensor
180-
KinectSensor.Open(); // Open 2nd
181-
for (var i = 0; i < 20; i++)
182-
{
183-
// Wait for the device to initialize
184-
Thread.Sleep(200);
185-
if (KinectSensor.IsAvailable) break;
186-
}
187-
}
188-
catch
189-
{
190-
// ignored
191-
}
192-
193-
// Register a watchdog (remove, add)
194-
KinectSensor.IsAvailableChanged -= StatusChangedHandler;
172+
// Register a sensor status event, new frame event
195173
KinectSensor.IsAvailableChanged += StatusChangedHandler;
174+
BodyFrameReader.FrameArrived += OnBodyFrameArrivedHandler;
196175

197176
// Check the status and return
198177
return KinectSensor.IsAvailable;
@@ -210,31 +189,21 @@ private void StatusChangedHandler(object o, IsAvailableChangedEventArgs isAvaila
210189
Host?.RefreshStatusInterface();
211190
}
212191

213-
private void InitializeSkeleton()
214-
{
215-
BodyFrameReader?.Dispose();
216-
BodyFrameReader = KinectSensor.BodyFrameSource.OpenReader();
217-
218-
if (BodyFrameReader is null) return;
219-
220-
// Register a watchdog (remove, add)
221-
BodyFrameReader.FrameArrived -= OnBodyFrameArrivedHandler;
222-
BodyFrameReader.FrameArrived += OnBodyFrameArrivedHandler;
223-
}
224-
225192
private void OnBodyFrameArrivedHandler(object _, BodyFrameArrivedEventArgs args)
226193
{
227194
var dataReceived = false;
228-
using var bodyFrame = args.FrameReference.AcquireFrame();
229-
if (bodyFrame != null)
195+
using (var bodyFrame = args.FrameReference.AcquireFrame())
230196
{
231-
Bodies ??= new Body[bodyFrame.BodyCount];
197+
if (bodyFrame != null)
198+
{
199+
Bodies ??= new Body[bodyFrame.BodyCount];
232200

233-
// The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array.
234-
// As long as those body objects are not disposed and not set to null in the array,
235-
// those body objects will be re-used.
236-
bodyFrame.GetAndRefreshBodyData(Bodies);
237-
dataReceived = true;
201+
// The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array.
202+
// As long as those body objects are not disposed and not set to null in the array,
203+
// those body objects will be re-used.
204+
bodyFrame.GetAndRefreshBodyData(Bodies);
205+
dataReceived = true;
206+
}
238207
}
239208

240209
// Validate the result from the sensor

0 commit comments

Comments
 (0)