6

Possible Duplicate:
Order of event handler execution

Is the C# event system deterministic for single-thread programs? That means, if I fire events A, B, and C in this order, will they be processed in the same order, every time?

I want to write a game logic which is heavily dependent on events, and it is crucial that the events are processed in exactly the order in which they are called. So can I use the given event system, does a library like Reactive Extensions satisfy this, or do I have to implement my own observer system?

Community
  • 1
  • 1
Hackworth
  • 1,069
  • 2
  • 9
  • 23
  • 2
    @Lloyd It's basically the same question, but it's over 3 years old, and the accepted answer says that it could change in the future - has it changed? – Hackworth Jan 09 '13 at 16:28
  • 2
    @Hackworth Is it the same question? That question has one event with several subscribers. Your question mentions multiple events. Which is it? Some sample code would help clarify. – Mike Zboray Jan 09 '13 at 16:32
  • @mikez It's a purely hypothetical question, no code has been written as of yet. If it does make a difference, please assume and answer for the worst case. – Hackworth Jan 09 '13 at 16:35
  • _"it is crucial that the events are processed in exactly the order in which they are called"_ -- events are not "called", they are "subscribed" and "raised". It's not clear which of those two you mean when you write "called", though the latter seems more likely. If that's the case, then the post your question was closed as a duplicate of, is not in fact a duplicate, and in fact has very little to do with your actual question. It would help if you would clarify what you actually meant; you never did when asked before, and I'd like to understand whether this question is worth reopening. – Peter Duniho Oct 27 '20 at 21:19

1 Answers1

3

[ for single-thread programs, ] if I fire events A, B, and C in this order, will they be processed in the same order, every time?

Yes. Firing an event is just a complicated way to call a method. So it's equivalent to:

On a single thread, if I call methods A(), B() and then C() will they execute in that order?

Of course they will.

H H
  • 263,252
  • 30
  • 330
  • 514
  • you do make a good point. On a single thread, events are (normally) user triggered function calls. When the user does this, call this function. And the will always execute in the order they are called. Adding multiple threads makes things interesting....Events will always execute in the order they are called BY THE THREAD CALLING THEM. if two different threads fire the same event, they will execute simultaneously, one on each thread, unless locking/blocking code is in place to allow only 1 instance at a time. – Nevyn Jan 09 '13 at 16:57