简体   繁体   English

在事件中创建新线程

[英]creating new thread in an event

I have created a thread within event sensor_DepthFrameReady but when I execute it throw an exception on line 8(Depths[depth]++;) in the function SetImage1Frame and throw this exception: 我已经在事件sensor_DepthFrameReady中创建了一个线程,但是当我执行该线程时,在函数SetImage1Frame中的第8行(Depths [depth] ++;)上抛出了一个异常,并抛出了这个异常:

Index was outside the bounds of the array.(depth = -1) 索引超出数组的范围。(深度= -1)

what is going wrong? 怎么了? I think that the function (SetImage1Frame ) is executing more than one time ,but I used this function only in this method(sensor_DepthFrameReady)!! 我认为函数(SetImage1Frame)正在执行多次,但是我仅在此方法中使用了此函数(sensor_DepthFrameReady)!

    void sensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
    {
        short[] pixels;
        Thread th;
        using (frame=e.OpenDepthImageFrame())
        {
            if (frame!=null)
            {
                pixels = new short[frame.PixelDataLength];
                frame.CopyPixelDataTo(pixels);
                th = new Thread(() =>
                    SetImage1Frame(pixels));
                th.Start();
            }
        }     

    }
    int[] Depths=new int[4096];
    int depth=0;
    double chartBarWidth;
    void SetImage1Frame(short[] dpixels)
    {
        int maxValue = 0;
        for (int i = 0; i < dpixels.Length; i++)
        {
            depth = dpixels[i] >> DepthImageFrame.PlayerIndexBitmaskWidth;
            if (depth > 0)
            {
                Depths[depth]++;
            }
        }
        for (int i = 0; i < Depths.Length; i++)
        {
            maxValue=Math.Max(maxValue,Depths[i]);
        }

        for (int i = 0; i < Depths.Length; i++)
        {
            if (Depths[i] > 0)
            {
                stackPanel1.Dispatcher.Invoke(new Action(() =>
                {
                    Rectangle r = new Rectangle();
                    stackPanel1.Children.Add(r);
                }));
            }
        }
        image1.Dispatcher.BeginInvoke(new Action(() =>
        {
            WrbitmapImage.WritePixels(rect, dpixels, stride, 0);
        }));

    }

Your code is not handling the signed numbers coming in from Kinect correctly. 您的代码无法正确处理来自Kinect的签名数字。 This program demonstrates the problem:- 该程序演示了该问题:

class Program
{
    static void Main(string[] args)
    {
        short s = unchecked((short)0x8001);
        int i = s >> 3;
        Console.WriteLine(i);
    }
}

This program will print -4096. 该程序将打印-4096。 The Kinect input is coming to you as an array of short, which is a signed type. Kinect输入以short数组形式出现,这是一个有符号类型。 When you shift away the player index, that number is being sign extended and remaining a negative number. 当您移开播放器索引时,该数字正被符号扩展并保持为负数。 In your case, since you're seeing -1, that is probably a pixel in the depth view which is at more than 4 metres away. 在您的情况下,由于看到的是-1,因此深度视图中的像素可能在4米以上。 To fix the problem, you should cast the depth pixel value to ushort before dealing with it and use only unsigned types. 要解决此问题,应在处理深度像素值之前将其深度像素值转换为ushort,并仅使用无符号类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM