I am trying to write kinect depth data to a text file using nested Parallel.For loops with the following code. However, it gives IndexOutofRangeException.
The code works perfect if using simple for loops but it hangs the UI since the depth format is set to 640x480 causing the loops to write 307200 lines in the text file at 30fps.
Therefore, I switched to Parallel. For scheme. If I omit the writeLine command from the nested loops, the code works fine, which indicates that the IndexOutofRangeException is arising at the writeline command. I do not know how to troubleshoot this. Please advise.
Any better workarounds to avoid UI freezing?
Thanks.
using (DepthImageFrame depthImageframe = d.OpenDepthImageFrame())
{
if (depthImageframe == null)
return;
depthImageframe.CopyPixelDataTo(depthPixelData);
swDepth = new StreamWriter(@"E:\depthData.txt", false);
int i = 0;
Parallel.For(0, depthImageframe.Width, delegate(int x)
{
Parallel.For(0, depthImageframe.Height, delegate(int y)
{
p[i] = sensor.MapDepthToSkeletonPoint(depthImageframe.Format,
x, y, depthPixelData[x + depthImageframe.Width * y]);
swDepth.WriteLine(i + "," + p[k].X + "," + p[k].Y + "," + p[k].Z);
i++;
});
});
swDepth.Close();
}
}