Working with Visual Studio 2015, WPF I have a Canvas control which serves as a canvas to draw a skeleton that I get by scanning through the Kinect device when I get in front of the device my skeleton that appears on the canvas freezes.
I tried using async / await using task to prevent it from freezing, this is the code I occupy.
//skeletonCanvas.Children.Clear();
await Task.Run(() => skeletonCanvas.Children.Clear());
//skeletonCanvas.Children.Add(jointLine);
await Task.Run(() => skeletonCanvas.Children.Add(jointLine));
Code of the entire event
private async void KSensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
string manoDerecha = string.Empty;
string manoIzquierda = string.Empty;
Skeleton[] skeletons;
using (var frame = e.OpenSkeletonFrame())
{
if (frame == null) return;
skeletons = new Skeleton[frame.SkeletonArrayLength];
frame.CopySkeletonDataTo(skeletons);
}
int iSkeleton = 0;
var brushes = new Brush[6];
brushes[0] = new SolidColorBrush(Color.FromRgb(255, 0, 0));
brushes[1] = new SolidColorBrush(Color.FromRgb(0, 255, 0));
brushes[2] = new SolidColorBrush(Color.FromRgb(64, 255, 255));
brushes[3] = new SolidColorBrush(Color.FromRgb(255, 255, 64));
brushes[4] = new SolidColorBrush(Color.FromRgb(255, 64, 255));
brushes[5] = new SolidColorBrush(Color.FromRgb(128, 128, 255));
//skeletonCanvas.Children.Clear();
await Task.Run(() => skeletonCanvas.Children.Clear());
foreach (var data in skeletons)
{
if (SkeletonTrackingState.Tracked == data.TrackingState)
{
Joint jointManoDerecha = data.Joints[JointType.HandRight];
Joint jointManoIzquierda = data.Joints[JointType.HandLeft];
SkeletonPoint posicionManoDerecha = jointManoDerecha.Position;
SkeletonPoint posicionManoIzquierda = jointManoIzquierda.Position;
manoDerecha = string.Format("Mano derecha: X:{0:0.0} Y:{1:0.0} Z{2:0.0}", posicionManoDerecha.X, posicionManoDerecha.Y,
posicionManoDerecha.Z);
manoIzquierda = string.Format("Mano izquierda: X:{0:0.0} Y:{1:0.0} Z{2:0.0}", posicionManoIzquierda.X, posicionManoIzquierda.Y,
posicionManoIzquierda.Z);
// Dibujar huesos
Brush brush = brushes[iSkeleton % brushes.Length];
skeletonCanvas.Children.Add(GetBodySegment(data.Joints, brush, JointType.HipCenter, JointType.Spine, JointType.ShoulderCenter, JointType.Head));
skeletonCanvas.Children.Add(GetBodySegment(data.Joints, brush, JointType.ShoulderCenter, JointType.ShoulderLeft, JointType.ElbowLeft, JointType.WristLeft, JointType.HandLeft));
skeletonCanvas.Children.Add(GetBodySegment(data.Joints, brush, JointType.ShoulderCenter, JointType.ShoulderRight, JointType.ElbowRight, JointType.WristRight, JointType.HandRight));
skeletonCanvas.Children.Add(GetBodySegment(data.Joints, brush, JointType.HipCenter, JointType.HipLeft, JointType.KneeLeft, JointType.AnkleLeft, JointType.FootLeft));
skeletonCanvas.Children.Add(GetBodySegment(data.Joints, brush, JointType.HipCenter, JointType.HipRight, JointType.KneeRight, JointType.AnkleRight, JointType.FootRight));
// Dibujar articulaciones
foreach (Joint joint in data.Joints)
{
Point jointPos = GetDisplayPosition(joint);
var jointLine = new Line();
jointLine.X1 = jointPos.X - 3;
jointLine.X2 = jointLine.X1 + 6;
jointLine.Y1 = jointLine.Y2 = jointPos.Y;
jointLine.Stroke = _jointColors[joint.JointType];
jointLine.StrokeThickness = 6;
//skeletonCanvas.Children.Add(jointLine);
await Task.Run(() => skeletonCanvas.Children.Add(jointLine));
}
}
iSkeleton++;
} // para cada esqueleto
lblManoDerecha.Content = manoDerecha;
lblManoIzquierda.Content = manoIzquierda;
}
The error he gives me is this:
The subprocess making the call can not access this object because the owner is another subprocess.