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
Question: how to implement asynchronous events rising which occurs in the right order?
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.: 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.: I don't know how to make MCVE to reproduce the problem. It appears in real project with heavy UI, lots of threads, etc.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…