Reputation: 491
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?
Upvotes: 4
Views: 263
Reputation: 1062530
The infamous captured-variable/foreach glitch; try:
foreach (Symbol symbol in Portfolio.Symbols)
{
var copy = symbol;
GenerateQuoteRequest += () => { SomeMethod(copy); };
}
and btw; static event
s are really dangerous - those event subscriptions won't unsubscribe themselves, so you could be keeping lots of things in memory unnecessarily. You can make them self-unsubscribing, of course:
foreach (Symbol symbol in Portfolio.Symbols)
{
var copy = symbol;
OnGenerateQuoteRequestEventHandler handler = null;
handler = () => {
SomeMethod(copy);
GenerateQuoteRequest -= handler;
};
GenerateQuoteRequest += handler;
}
Upvotes: 10