【发布时间】:2013-06-19 09:38:38
【问题描述】:
我正在开发一个从设备 (Kinect) 获取流的 C# 应用程序。
由于我的 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。
我认为我在处理线程中使用的对象时是错误的。
谁能帮我找出这些错误?
【问题讨论】:
-
队列为空时,在休眠的 2 秒内可以添加多少项? (我认为您应该为此使用
BlockingCollection而不是队列...) -
2 秒内 20 个项目。因为我收到了超过三帧的一帧。我正在使用
ConcurrentQueue<T>
标签: c# .net dispose idisposable