I'm not sure if this a common scenario or not, but I have a need along the following lines:

I have an object (I'll call it node) and this object has methods X, Y, and Z. I want to be able to have subscribers be notified if the methods are called in a specific order with specific timing (a range of time would be ideal). For instance:

  1. Node.X() get called
  2. Node.Z() is called 5 to 7 milliseconds latter
  3. Node.X() is called 1 to 2 milliseconds after that
  4. Node.Y() is called 2 milliseconds seconds after that.
  5. Finally, An event calling a subscriber to this sequence is fired.

Now, in this example I am simply using parameter-less methods, but in real life I'd need to be more specific about the contents of the parameters (I'd be looking for specific messages).

For reference I am using C# .net 4.

Are there any frameworks/patterns/libraries that exist out there that are built for doing this?

  • 2
    How are these methods going to be called? It seems like something in your system is responding to asynchronous external events. Oct 3, 2011 at 14:42
  • @kevincline that is somewhat up in the air right now (research phase) but the plan now is that each node would be hosted within a WCF process and other nodes would talk to each other via asynchronous named pipes calls. Oct 3, 2011 at 14:52
  • Seeks like this SO question on implementing actors in C# might be relevant Oct 3, 2011 at 14:59
  • @all for context, we are attempting to build a monitoring tool that uses a type of adaptive pattern recognition that will identify and subscribe to various events (this is a piece of that) as they happen in real time. Oct 3, 2011 at 15:00
  • @aceinthehole Are you referring to complex event processing or event stream processing? I'm not sure about particular patterns, but (as of a couple of years ago) the canonical book on the subject is David Luckham's The Power of Events (2002). There are probably more resources and libraries out there that are newer if you search around for the terms I mentioned.
    – Thomas Owens
    Oct 3, 2011 at 16:54

4 Answers 4


I don't know of a framework or a library that can handle this out of the box, but one solution to your problem is implementing a finite state machine for event generation. As your example suggests, the FSM has 5 states. The transitions from i to i+1 are your conditions on your list. Every time a node function is called, you can pass in the event to the FSM, and it can check if the condition for state i is satisfied, and adjust state accordingly (condition holds => go to state i+1; else go to state 1). If you reach state 5, emit an event, and go back to state 1.

For example, if you are on state 3, and you receive EVENT_X_CALLED, then you check the current time, and compare it against transition time. If it is 1 to 2 ms apart, change state to 4. If not, go back to state 1.


I have used Microsoft PRISM's EventAggregator or MVVM Light's Messenger for message systems like this, and I would assume you'd have a class that subscribes to all 3 events and routes them to the same method.

In that method, check the message and see if it is the first type that handler is interested in. If yes, set a flag and a timer (optional) and wait for the next one. When you receive the 2nd event, check if the last received event was Event1 and if so set last received Event to Event2 and reset timer again. Repeat until you get all your events, and then process.

Here's a rough example:

public MyClassConstructor()

private int eventCount = 0;
void ProcessEvents(object e)
    if ((e is EventX && eventCount == 0)
        || (e is EventZ && eventCount == 1)
        || (e is EventX && eventCount == 2)
        // optionally start/reset timer which sets eventCount = 0
    else if (e is EventY && eventCount == 3)
        // Execute code
        eventCount = 0;

Each Node.Method would be responsible for firing off an event message with parameters based on which method got executed

eventAggregator.Publish<EventX>(new EventX(params));

.NET (and ultimately, out of the box Windows) may not be your best option for this.

All events are going to have a significant amount of wiggle room as to when they are addressed by the operating system this SO thread goes into a bit of the background.


The Reactive Extensions for .NET could possibly be used to implement the scenario you describe. You can think of them as "LINQ to events". Check out the samples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.