“There is an event that fires whenever a new frame is received from the camera. However,this happens more frequently than I’d like … How can I control when the event fires?”
在我的回答中,我提供了下面的代码,今天早上我发现我有2个downVotes没有任何评论.我担心的主要不是代表的丢失,而是我在各种应用程序中使用自己的逻辑,并且downVotes可能表明我的实现是糟糕的实践或不利于性能.因此,我问这个问题是为了澄清以这种方式附加/分离事件控制器是否有任何问题?
public MyObject() { MyTimer = new System.Timers.Timer(100); // 10 Hz MyTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); MyTimer.Enabled = true; } private void ImageDataUpdated(object sender,EventArgs e) { // detach from the event to keep it from firing until the timer event has fired. MyImageObject.Update -= new UpdateEventHandler(ImageDataUpdated); // do stuff } private static void OnTimedEvent(object source,ElapsedEventArgs e) { // (re-)attach to the event handler. MyImageObject.Update += new UpdateEventHandler(ImageDataUpdated); }
此外,我写了以下内容,指的是在实际准备要发送给订阅者的数据之前,可以检查您是否有任何订阅者.在某些情况下,这可能会导致cpu使用率降低.对我来说似乎是对的(我自己也这样做了),但这句话有什么不对吗?
Using this strategy there is a good chance that you are preventing the underlying image object from doing additional work while the event handler is detached (of course this depends on the implementation of the image object). Chances are that you are saving cpu-cycles for your own image processing.
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。