I have a method Foo.LongRunningMethod()
, which does some very complicated processing that may go on for a long time. Along the way, it fires Foo.InterestingEvent
whenever it encounters a certain condition. I'd like to be able to expose an enumeration of those events, and I'd like to be able to start iterating before LongRunningMethod
actually finishes. In other words, what I want is something like this:
public IEnumerable<InterestingObject> GetInterestingObjects()
{
foo.InterestingEvent += (obj) => { yield return obj; }
foo.LongRunningMethod();
yield break;
}
This doesn't work, though, for the sensible reason that you can't yield return
from an anonymous method (and because a method using yield
cannot return void
, which our event handler does). Is there another idiom that allows me to accomplish this? Or is this just a bad idea?