我正在开发一个 C# 应用程序,它获取来自设备(Kinect)的流。
由于我的 CPU 的帧速率太高,我尝试使用线程。这似乎解决了我的问题。每次收到新帧时,我都会将其放入队列中,然后另一个线程执行出列并将帧写入文件中。
该线程的代码如下:
private void myThread()
{
writer1 = new VideoFileWriter();
writer1.Open(outputFile1, 320, 240, 10, VideoCodec.WMV2);
while (!queue.IsEmpty || !streamClosed)
{
ColorImageFrame item = null;
if (!queue.IsEmpty && queue.TryDequeue(out item))
{
if (item != null)
{
Bitmap result = new Bitmap(320, 240);
using (Graphics g = Graphics.FromImage(result))
{
g.DrawImage(ImageToBitmap(item), 0, 0, 320, 240);
writer1.WriteVideoFrame(result);
}
}
Console.WriteLine("Queue size: "+queue.Count);
}
try
{
item.Dispose();
}
catch (NullReferenceException ex)
{
Console.WriteLine(ex.Message);
}
if (queue.IsEmpty)
{
System.Threading.Thread.Sleep(2000);
}
}
writer1.Close();
Environment.Exit(0);
}
这似乎运作良好,但有时我会收到一个OutOfMemoryException
.
我认为我在处理线程中使用的对象时是错误的。
有人可以帮我找到这些错误吗?