【发布时间】:2011-05-16 06:16:59
【问题描述】:
我有带有一些后台处理的多线程应用程序。它具有长时间运行的 UI 更新(在 UI 线程本身上),这些更新是通过众所周知的 resource on MSDN 从后台线程调用的。我无法缩短这些 UI 更新,因为它们最终是在外部库中完成的 (1)。
现在,从那个后台线程,我想在 UI 线程上异步调用(使用BeginInvoke())这些更新,但前提是之前的更新已经完成。如果没有,我会喜欢简单地跳过此更新。这将防止 Windows 消息队列溢出,以防调用的速度快于被调用方法能够执行的速度。
我目前的解决方案是这样的: 在 UI 线程上执行的方法中,我确实进入和退出了 ReaderWriterLockSlim 实例。在后台线程上,我尝试以零超时进入实例。成功后,我调用“BeginInvoke()”然后再次退出。如果不成功,我会完全跳过方法调用。
public void Update(double x, double y)
{
_updateLock.EnterWriteLock();
try
{ //...long running task... }
finally
{ _updateLock.ExitWriteLock(); }
}
//....
void Provider_PositionChanged(object sender, SpecialEventArgs e)
{
if (_updateLock.TryEnterWriteLock(0)) //noone is currently executing an update?
{
try { myUiControl.BeginInvoke(/*...*/); }
finally { _updateLock.ExitWriteLock(); }
}
这一切都有效,但有更优雅的解决方案吗?如何从一个线程简单地测试一个方法是否在任何(其他)线程上执行?
- 注意:使用
Invoke()(而不是BeginInvoke())不是一个选项,因为这会阻塞我的后台线程,阻止其他东西在那里执行。 - (1)它是 MapXtreme,一种映射解决方案,我想平移/缩放大型位图地形数据,并更新一些功能。
- PS。这个问题有点相关,但涵盖了不同的方面:Winforms multithreading: Is creating a new delegate each time when invoking a method on the UI thread needed?
感谢您的任何回答!
更新: Hans Passant 帮助我回答了问题。 请参阅下面的解决方案。希望这对其他人也有帮助。
/// <summary>
/// This class enqueues asynchronously executing actions (that are running on another thread), but allows
/// to execute only one action at a time. When busy, newly enqueued actions are dropped.
/// Any enqueued action is required to call Done() on this when it has finished, to allow further actions
/// to execute afterwards.
/// </summary>
/// <remarks>This class is intended to help prevent stacking UI-Updates when the CPU or other resources
/// on the machine are not able to handle the amount of updates requested. However, the user
/// must keep in mind, that using this class may result
/// in dropped updates and that the last requested update is not always executed.</remarks>
public class ActionBouncer
{
/// <summary>
/// A event that signals the idle/busy state. Idle means, that no action is currently executing.
/// </summary>
private ManualResetEvent _idle = new ManualResetEvent(true);
/// <summary>
/// Enqueues the specified action, executing it when this bouncer
/// is currently idle.
/// </summary>
/// <param name="action">The action.</param>
public void Enqueue(Action action)
{
if (_idle.WaitOne(0)) //are we idle now? (Remark: This check and the reset below is not thread-safe (thanks to s.skov))
{
_idle.Reset(); //go to busy state
action(); //execute the action now.
}//else drop the action immediately.
}
/// <summary>
/// Signal the bouncer, that the currently executing asynchronous action is done, allowing
/// subsequent requests to execute.
/// This must get explicitly called (in code) at the end of the asynchronous action.
/// </summary>
public void Done()
{
_idle.Set();
}
}
【问题讨论】:
-
但它不是线程安全的,除非您锁定对 Enqueue 的访问。 _idle.WaitOne 和 _idle.Reset 之间可以有多个线程。对于您的情况,这似乎不是问题,但如果您有多个工作线程调用 Enqueue,则值得了解 - 可能更新代码注释以反映这一点。
-
@S.Skov:您对 Enqueue() 线程安全的评论是正确的。正如您猜对的那样,这对我的应用程序来说不是问题,因为工作线程总是相同的。但是,这应该得到解决!
标签: c# winforms multithreading asynchronous methods