Basically I have an anonymous method that I use for my BackgroundWorker
:
worker.DoWork += ( sender, e ) =>
{
foreach ( var effect in GlobalGraph.Effects )
{
// Returns EffectResult
yield return image.Apply (effect);
}
};
When I do this the compiler tells me:
"The yield statement cannot be used inside an anonymous method or lambda expression"
So in this case, what's the most elegant way to do this? Btw this DoWork method is inside a static method, in case that matters for the solution.
Ok so I did something like this which does what I wanted (some variables omitted):
and then in the call site:
Unless I'm missing something, you can't do what you're asking.
(I do have an answer for you, so please read past my explanation of why you can't do what you're doing first, and then read on.)
You full method would look something like this:
If we assume that your code was "legal" then when
GetSomeValues
is called, even though theDoWork
handler is added toworker
, the lambda expression isn't executed until theDoWork
event is fired. So the call toGetSomeValues
completes without returning any results and the lamdba may or may not get called at a later stage - which is then too late for the caller of theGetSomeValues
method anyway.Your best answer is to the use Rx.
Rx turns
IEnumerable<T>
on its head. Instead of requesting values from an enumerable, Rx has values pushed to you from anIObservable<T>
.Since you're using a background worker and responding to an event you are effectively having the values pushed to you already. With Rx it becomes easy to do what you're trying to do.
You have a couple of options. Probably the simplest is to do this:
Now callers of your
GetSomeValues
method would do this:If you know that
DoWork
is only going to fire once, then this approach might be a little better:This code looks a little more complicated, but it just turns a single do work event into a stream of
EffectResult
objects.Then the calling code looks like this:
Rx can even be used to replace the background worker. This might be the best option for you:
The calling code is the same as the previous example. The
Scheduler.ThreadPool
tells Rx how to "schedule" the processing of subscriptions to the observer.I hope this helps.
DoWork
is of typeDoWorkEventHandler
which returns nothing (void
), so it's not possible at all in your case.Unfortunately you can't.
The compiler does not allow you to combine the two "magic" pieces of code. Both involve rewriting your code to support what you want to do:
You can, however, rewrite the code to return the collection, so in your particular case I would do this:
though it looks odd for an event
(sender, e)
to return anything at all. Are you sure you're showing a real scenario for us?Edit Ok, I think I see what you're trying to do here.
You have a static method call, and then you want to execute code in the background, and then return data from that static method once the background call completes.
This is, while possible, not a good solution since you're effectively pausing one thread to wait for another, that was started directly before you paused the thread. In other words, all you're doing is adding overhead of context switching.
Instead you need to just kick off the background work, and then when that work is completed, process the resulting data.
Perhaps just return the linq expression and defer execution like yield:
The worker should set the Result property of DoWorkEventArgs.