有多长)Control.Invoke(之间的延迟及其委托的调用? [英] How long is the delay between Control.Invoke() and the calling of its Delegate?

查看:258
本文介绍了有多长)Control.Invoke(之间的延迟及其委托的调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过播放使用waveOutOpen和waveOutWrite API方法陆续小块播放很长的WAV文件的code发动机。为了每个缓冲完成打我调用一个单独的线程(因为你想要做的尽可能少的回调函数中)来更新我的UI作为文件的播放,从回调函数调用在我的窗体的方法。

I have a code engine that plays long WAV files by playing smaller chunks in succession using the waveOutOpen and waveOutWrite API methods. In order to update my UI as the file plays, from the callback function as each buffer completes playing I Invoke a separate thread (because you want to do as little as possible inside the callback function) that calls a method in my form.

该表单包含一个类级别的事件处理程序处理范围内,我更新了新的信息的UI元素的方法。从waveOutWrite回调函数调用的形式方法,我用的是Invoke方法如下所示:

The form contains a class level EventHandler that handles a method within which I update UI elements with new information. In the form method called from the waveOutWrite callback function, I use the Invoke method like so:

if (_updatedisplay == null)
{
    // UpdateDisplay contains code to set control properties on the form
    _updatedisplay = new EventHandler(UpdateDisplay);
}
Invoke(_updatedisplay);

万物的工作,但现在看来,曾经在一段时间没有在UI元素的更新明显滞后或延迟。这是很容易看到,因为我使用的执行UpdateDisplay方法来驱动动画,所以延迟出现打嗝里的精灵出现冻结​​不到一秒钟,才跳转到预期的位置。

Everythings works, but it appears that once in a while there is a noticeable lag or delay in the updating of the UI elements. This is easy to see because I am using the UpdateDisplay method to drive an animation, so the delays appear as "hiccups" where the sprite appears to freeze for a split second before it jumps to its expected position.

是否有可能存在有时涉及跨线程通信这样的长(也许10至15毫秒)延迟?如果是这样,有什么处理这样的事情更好的办法?

Is it possible that there is sometimes a long (maybe 10-15 milliseconds) delay involved in cross-thread communication like this? If so, what's a better way of handling something like this?

更新:对了,我是绝对的没有的确认调用是这里的罪魁祸首。另一种可能性是,当音频的一大块播放完时的回调函数实际上被调用之间的延迟。

Update: by the way, I'm definitely not sure that Invoke is the culprit here. Another possibility is a lag between when a chunk of audio finishes playing and when the callback function actually gets called.

更新2 :每 itowlson 的建议下,我用了一个 System.Diagnostics.Stopwatch 至基准调用之间的时间差和方法调用。超出1156测试,我得到了1146,在0毫秒,8毫秒时,2个在2毫秒。我认为这是肯定地说调用是不是在这里我的罪魁祸首。

Update 2: per itowlson's suggestion, I used a System.Diagnostics.Stopwatch to benchmark the lag between Invoke and method call. Out of 1156 measurements, I got 1146 at 0ms, 8 at 1ms, and 2 at 2ms. I think it's safe to say Invoke is not my culprit here.

推荐答案

是的,有可能是一个任意长的延迟。调用通过发送Windows消息到目标控制,因此当目标线程泵消息只会得到处理。如果线程已经在处理信息,并处理需要时间,那么有可能是一个AP preciable延迟线程泵之前,它的下一条消息,从而处理调用。

Yes, there can be an arbitrarily long delay. Invoke works by sending a Windows message to the target control, so it will only get processed when the target thread pumps messages. If the thread is already processing a message, and that processing takes time, then there may be an appreciable delay before the thread pumps its next message and thereby processes the Invoke.

有一个更好的办法可能是调用BeginInvoke。这并不能避免在UI线程处理消息的潜在延迟,但它可以节省您的电话线程在等待UI线程泵消息被封锁。但是,这可能不是你的情况帮它听起来就像是忙碌的烦躁的UI线程这也是造成失灵的动画。

A better way may be to call BeginInvoke. This doesn't avoid the potential delay in the UI thread processing the message, but it saves your calling thread from being blocked while waiting for the UI thread to pump messages. However, this may not help in your scenario where it sounds like it's the busy-ness of the UI thread which is causing glitching in the animation.

更新响应您的更新:注意所有我想说的是,还有的可以的是一个不定长的延迟,不存在的是一个明显的延迟或者说这绝对是你延迟的原因。 10-15ms似乎特别长的时间,一个应用程序花费在消息处理,除非有一些真正密集发生在UI线程上,所以你肯定是明智的考虑其他原因!

Update in response to your update: Note all I'm saying here is that there could be an arbitrarily long delay, not that there will be a noticeable delay or that this is definitely the cause of your delay. 10-15ms does seem an unusually long time for an application to spend in message processing unless there's something really intensive happening on the UI thread, so you're certainly wise to consider alternative causes!

这篇关于有多长)Control.Invoke(之间的延迟及其委托的调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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