dispatcherTimer无法正常工作 [英] dispatcherTimer doesn't work accurately

查看:212
本文介绍了dispatcherTimer无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在15分钟内每秒捕获10帧实时摄像机。我使用了dispatcherTimer类,但似乎无法正常运行。如何使其准确?我什至不保存图像,因为它很耗时,只是将其保存在内存中,但仍然无法正常工作。
我应该使用其他图书馆吗?

I need to capture 10 frames per second of a live camera for 15 min. I used dispatcherTimer class but seems it doesn't work accurately. How to make it accurate? I even don't save the image because it is time consuming and i just keep it in the memory but still it doesn't work correctly. should I use another library?

counter01 = new DispatcherTimer();
counter01.Tick += new EventHandler(counter01_Tick);
counter01.Interval = new TimeSpan(0, 0, 0,100); // every 100 milliseconds == 10 frames per seconds


推荐答案

它和 一样准确。这要求您的UI线程具有响应能力,并迅速分派它收到的所有消息。包括DispatcherTimer生成的时间已到通知,即那些生成Tick事件的通知。

It is as accurate as it can be. Which requires your UI thread to be responsive, quickly dispatching any messages it gets. Including the "time is up" notifications that DispatcherTimer generates, the ones that generate the Tick event.

通常会看到DispatcherTimer延迟触发Tick事件处理程序,这是您的麻烦有个水喉问题。换句话说,事件处理程序所需的时间长于计时器的Interval属性值 。这样会按照您期望的方式自我保护,每当您尝试做的事情超出机器的处理能力时,您尝试做的任何事情都将在以后运行。

The usual hangup with seeing DispatcherTimer triggering the Tick event handler late is that you have a firehose problem. In other words, the amount of time required by the event handler is longer than the timer's Interval property value. This takes care of itself in the manner you expect, whenever you try to do more than the machine can handle then whatever you are trying to do just runs later.

对于视频的烟火问题,一帧可以包含很多数据。典型的hidef视频流以每秒25帧的速度运行,每帧包含1920x1080像素。那是25 x 1920 x 1080 x 3 = 148兆字节/秒。除了减少开销之外,您甚至无法以该速率将数据写入磁盘。大多数消费级磁盘驱动器的最高速度为30兆字节/秒,给定或乘以2。将帧速率降低到10 /秒是不足以解决的,仍然是60 MB /秒,您必须付出更多的努力。诸如减小框架尺寸的措施。或使用可以实时压缩视频的优质视频编码器。

No lack of firehose problems with video, one frame can contain a lot of data. A typical hidef video stream runs at 25 frames per second with each frame containing 1920x1080 pixels. That's 25 x 1920 x 1080 x 3 = 148 megabytes/second. Short from decompressing overhead, you can't even write the data to a disk at that rate. Most consumer-level disk drives top out at 30 megabytes/second, give or take a factor of 2. Lowering the frame rate to 10/sec is not enough to solve it, that's still 60 MB/sec, you have to take more drastic measures like reducing the frame size. Or use a good video encoder that can compress video real-time.

这是系统性问题,而不是DispatcherTimer问题。使用探查器获得更多见解,它可以帮助您确定真正的瓶颈。

This is a systemic problem, not a DispatcherTimer problem. Use a profiler to get more insight, it helps you identify the true bottle-neck.

这篇关于dispatcherTimer无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆