上升的事件没有按正确的顺序封锁和接收事件 [英] Rising events without blocking and receiving events in the right order

查看:119
本文介绍了上升的事件没有按正确的顺序封锁和接收事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这需要一些解释。有一个工作线程必须发生一些事件:

This required some explanation first. There is a working thread which has to rise some event:

Task.Run(() =>
{
    for(int i = 0; i < 123456789; i++)
    {
        ... // some job
        OnSomeEvent(i);
    }
});

同步上升的事件将阻止该作业,直到所有事件处理程序完成:

Rising events synchronously will block the job until all event handlers have finished:

void OnSomeEvent(int i) => SomeEvent?.Invoke(this, new SomeEventArgs(i));

异步事件上升不会阻止该工作(yay!)

Asynchronous event rising will not block the job anymore (yay!)

void OnSomeEvent(int i) => Task.Run(() => SomeEvent?.Invoke(this, new SomeEventArgs(i)));

但现在还有另一个问题:事件没有按正确的顺序收到:

but now there is another problem: events are not received in the right order:

OnSomeEvent(1);
OnSomeEvent(2);
OnSomeEvent(3);
...

// event handler
SomeEvent += (s, e) => Console.WriteLine(e.I);

// possible output
1
3
2

问题:如何实现以正确的顺序发生的异步事件?

最近我学到了什么 Dispatcher.InvokeAsync 使用队列。看起来我要做类似的事情。如果我必须:1)应该是一个呼叫者的工作,还是2)应该同步保持上升的事件,并且接收者必须组织生产者/消费者以防止工作被阻止?或者也许有另一种方式?

Recently I learned what Dispatcher.InvokeAsync uses queue. It looks like I have to do something similar. And if I have to then: 1) should it be a job of a caller or 2) should I keep rising events synchronously and receiver has to organize producer/consumer to prevent job from blocking? Or maybe there is another way?

PS:这与 ContinueWhith ..无关,除非存储任务是一个正确的解决方案我的关注是如何实施火灾忘记事件,其中:a)呼叫者不被阻止2)事件以相同的顺序被接收。

P.S.: this has nothing to do with ContinueWhith.. unless storing list of tasks is a proper solution. My concern is how to implement fire-and-forget events where: a) caller is not blocked 2) events are received in same order.

PPS:我不知道如何使MCVE重现问题。它出现在真实的项目中,重量级的UI,大量的线程等。

P.P.S.: I don't know how to make MCVE to reproduce the problem. It appears in real project with heavy UI, lots of threads, etc.

推荐答案

您可以使用以下 TaskQueue ,以便为队列添加异步操作,以便在队列中的前一个项目完成时启动每个操作:

You can use the following TaskQueue in order to add asynchronous operations to a queue so that each one is started when the previous item in the queue finishes:

public class TaskQueue
{
    private Task previous = Task.FromResult(false);
    private object key = new object();

    public Task<T> Enqueue<T>(Func<Task<T>> taskGenerator)
    {
        lock (key)
        {
            var next = previous.ContinueWith(t => taskGenerator()).Unwrap();
            previous = next;
            return next;
        }
    }
    public Task Enqueue(Func<Task> taskGenerator)
    {
        lock (key)
        {
            var next = previous.ContinueWith(t => taskGenerator()).Unwrap();
            previous = next;
            return next;
        }
    }
}

这允许你写:

private TaskQueue taskQueue = new TaskQueue();
private void OnSomeEvent(int i) => 
    taskQueue.Enqueue(() => Task.Run(() => SomeEvent?.Invoke(this, new SomeEventArgs(i))));

这篇关于上升的事件没有按正确的顺序封锁和接收事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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