I'm working on a program which reacts to events coming from an internet socket, and possibly from timers as well. It seems natural to use two threads:
- One for the main program
- A second one which listens to the socket, parses the input, and raises an appropriate event.
Additional requirements:
- The application should not rely on a UI thread (it may be run as a console application).
- The main program should process messages synchronously, i.e. in the order in which they arrived.
- The main thread must not block on waiting for timers (I guess this means I have to run timers on different threads).
And now for some questions :-):
- I'm guessing requirement #1 means that I don't have a built-in message pump, so I can't use
Invoke()
from the socket listener / timer threads. Is this correct? - How can I safely raise events on one thread (e.g. the listener), and have the subscribers run synchronously on another (the main thread)?
- It is very likely that new events will be raised before the subsequent handler is done. What will happen in this case? Will the event be buffed somewhere by the CLR, or will it be ignored?
And last but not least: I guess I'm aiming for the parallel for the message Producer/Consumer paradigm, but instead of messages, I want to use events. Do you think there is a better approach?
Thanks,
Boaz
EDIT
I want to explain my motivation for using events in the first place. The application is an automated trading engine which has to respond to events that happen in the market (e.g. a change in the price of a stock). When this happens, there may be multiple subscribers on the main thread which should be invoked, which is a classical scenario to use events.
I guess I can always use the Producer/Consumer with some message queue, and have the consumer raise events on the main thread, but I figured there might be a more direct way.