不阻塞的上升事件并以正确的顺序接收事件 [英] Rising events without blocking and receiving events in the right order

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

问题描述

这首先需要一些解释.有一个工作线程必须引发一些事件:

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));

异步事件上升将不再阻塞作业(耶!)

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?

P.S.:这与 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.

P.P.S.:我不知道如何让 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天全站免登陆