13

在 WPF 应用程序中,我有一个通过网络接收消息的类。每当所述类的对象收到完整消息时,就会引发一个事件。在应用程序的 MainWindow 中,我有一个订阅该事件的事件处理程序。保证在应用程序的 GUI 线程上调用事件处理程序。

每当调用事件处理程序时,都需要将消息的内容应用于模型。这样做可能会非常昂贵(在当前硬件上 > 200 毫秒)。这就是为什么使用 Task.Run 将应用消息卸载到线程池的原因。

现在,可以非常接近地连续接收消息,因此可以在仍在处理先前的更改时调用事件处理程序。确保一次只应用一条消息的最简单方法是什么?到目前为止,我想出了以下几点:

using System;
using System.Threading.Tasks;
using System.Windows;

public partial class MainWindow : Window
{
    private Model model = new Model();
    private Task pending = Task.FromResult<bool>(false);

    // Assume e carries a message received over the network.
    private void OnMessageReceived(object sender, EventArgs e)
    {
        this.pending = ApplyToModel(e);
    }

    private async Task ApplyToModel(EventArgs e)
    {
        await this.pending;
        await Task.Run(() => this.model.Apply(e)); // Assume this is an expensive call.
    }
}

这似乎按预期工作,但似乎这将不可避免地产生“内存泄漏”,因为应用消息的任务将始终首先等待应用前一条消息的任务。如果是这样,那么以下更改应避免泄漏:

private async Task ApplyToModel(EventArgs e)
{
    if (!this.pending.IsCompleted)
    {
        await this.pending;
    }

    await Task.Run(() => this.model.Apply(e));
}

这是避免异步 void 事件处理程序重入的明智方法吗?

编辑:删除了不必要的await this.pending;语句OnMessageReceived

编辑 2:消息必须以与接收消息相同的顺序应用于模型。

4

2 回答 2

12

我们需要在这里感谢 Stephen Toub,因为他在博客系列中展示了一些非常有用的异步锁定结构,包括异步锁定块。

以下是那篇文章的代码(包括本系列上一篇文章中的一些代码):

public class AsyncLock
{
    private readonly AsyncSemaphore m_semaphore;
    private readonly Task<Releaser> m_releaser;

    public AsyncLock()
    {
        m_semaphore = new AsyncSemaphore(1);
        m_releaser = Task.FromResult(new Releaser(this));
    }

    public Task<Releaser> LockAsync()
    {
        var wait = m_semaphore.WaitAsync();
        return wait.IsCompleted ?
            m_releaser :
            wait.ContinueWith((_, state) => new Releaser((AsyncLock)state),
                this, CancellationToken.None,
                TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
    }

    public struct Releaser : IDisposable
    {
        private readonly AsyncLock m_toRelease;

        internal Releaser(AsyncLock toRelease) { m_toRelease = toRelease; }

        public void Dispose()
        {
            if (m_toRelease != null)
                m_toRelease.m_semaphore.Release();
        }
    }
}

public class AsyncSemaphore
{
    private readonly static Task s_completed = Task.FromResult(true);
    private readonly Queue<TaskCompletionSource<bool>> m_waiters = new Queue<TaskCompletionSource<bool>>();
    private int m_currentCount;

    public AsyncSemaphore(int initialCount)
    {
        if (initialCount < 0) throw new ArgumentOutOfRangeException("initialCount");
        m_currentCount = initialCount;
    }
    public Task WaitAsync()
    {
        lock (m_waiters)
        {
            if (m_currentCount > 0)
            {
                --m_currentCount;
                return s_completed;
            }
            else
            {
                var waiter = new TaskCompletionSource<bool>();
                m_waiters.Enqueue(waiter);
                return waiter.Task;
            }
        }
    }
    public void Release()
    {
        TaskCompletionSource<bool> toRelease = null;
        lock (m_waiters)
        {
            if (m_waiters.Count > 0)
                toRelease = m_waiters.Dequeue();
            else
                ++m_currentCount;
        }
        if (toRelease != null)
            toRelease.SetResult(true);
    }
}

现在将其应用于您的案例:

private readonly AsyncLock m_lock = new AsyncLock();

private async void OnMessageReceived(object sender, EventArgs e)
{
    using(var releaser = await m_lock.LockAsync()) 
    {
        await Task.Run(() => this.model.Apply(e));
    }
}
于 2013-01-22T17:36:42.677 回答
1

给定一个使用异步等待的事件处理程序,我们不能在任务之外使用锁,因为每个事件调用的调用线程都是相同的,所以锁总是让它通过。

var object m_LockObject = new Object();

private async void OnMessageReceived(object sender, EventArgs e)
{
    // Does not work
    Monitor.Enter(m_LockObject);

    await Task.Run(() => this.model.Apply(e));

    Monitor.Exit(m_LockObject);
}

但是我们可以锁定在 Task 内部,因为 Task.Run 总是会生成一个新的 Task,它不会在同一个线程上并行运行

var object m_LockObject = new Object();

private async void OnMessageReceived(object sender, EventArgs e)
{
    await Task.Run(() => 
    {
        // Does work
        lock(m_LockObject)
        {
            this.model.Apply(e);
        }
    });
}

因此,当一个事件调用 OnMessageReceived 时,它会立即返回,并且 model.Apply 只会一个接一个地输入。

于 2014-04-04T13:31:51.737 回答