I have the following code I use to simulate a live data feed which simultaneously sends a message that each object of type "Symbol" in the collection inside "Portfolio.Symbols" should respond to (by another method doing some work on it).
In order for it to be true simultaneously, I try to register an anonymous event handlers the following way:
static public void RegisterEvents()
{
foreach (Symbol symbol in Portfolio.Symbols)
{
GenerateQuoteRequest += () => { SomeMethod(symbol); };
}
}
static public void Run()
{
OnGenerateQuoteRequest();
Thread.Sleep(100);
}
public delegate void OnGenerateQuoteRequestEventHandler();
public static event OnGenerateQuoteRequestEventHandler GenerateQuoteRequest
= delegate {};
...
I then try to raise the event, hoping the I will get a number of "SomeMethod" instances firing up. Unfortunately, only the last "symbol" added is called.
What am I missing here?