本文介绍了如何得到回报的内部匿名方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有我使用的匿名方法我的BackgroundWorker

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:

yield语句不能使用  匿名方法或lambda内  EX pression

因此​​,在这种情况下,什么是最优雅的方式来做到这一点?顺便说一句这DoWork的方法是一个静态方法里面,以免事项的解决方案。

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):

public static void Run ( Action<float, EffectResult> action )
{
    worker.DoWork += ( sender, e ) =>
    {
        foreach ( var effect in GlobalGraph.Effects )
        {
            var result = image.Apply (effect);

            action (100 * ( index / count ), result );
        }
    }
};

然后在调用点:

and then in the call site:

GlobalGraph.Run ( ( p, r ) =>
    {
        this.Progress = p;
        this.EffectResults.Add ( r );
    } );

这篇关于如何得到回报的内部匿名方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:08