28

我将使用匿名方法创建一个BackgroundWorker 。
我写了以下代码:

BackgroundWorker bgw = new BackgroundWorker();
bgw.DoWork += new DoWorkEventHandler(
    () =>
    {
        int i = 0;
        foreach (var item in query2)
        {
            ....
            ....
        }
    }
);


但是委托'System.ComponentModel.DoWorkEventHandler'不接受'0'参数,我必须将两个对象传递给匿名方法:对象发送者,DoWorkEventArgs e

你能指导我吗,我该怎么做?谢谢。

4

4 回答 4

57

您只需向匿名函数添加参数:

bgw.DoWork += (sender, e) => { ... }

或者,如果您不关心参数,您可以:

bgw.DoWork += delegate { ... }
于 2010-01-16T15:11:48.917 回答
33

如果指定 lambda,则必须确保它采用相同数量的参数:

bgw.DoWork += (s, e) => ...;

但是,如果您不使用参数,则可以只使用不带参数的匿名委托:

bgw.DoWork += delegate
{
    ...
};
于 2010-01-16T15:13:46.057 回答
4

如果您在没有 lambda 的情况下编写了上述内容,那会怎样?

backgroundWorker1.DoWork += 
                new DoWorkEventHandler(backgroundWorker1_DoWork);

和命名方法:

private void backgroundWorker1_DoWork(object sender, 
        DoWorkEventArgs e)
    {   
        // Get the BackgroundWorker that raised this event.
        BackgroundWorker worker = sender as BackgroundWorker;

        // Assign the result of the computation
        // to the Result property of the DoWorkEventArgs
        // object. This is will be available to the 
        // RunWorkerCompleted eventhandler.
        e.Result = ComputeFibonacci((int)e.Argument, worker, e);
    }

但是现在您使用的 lambdas 没有绑定变量 ()=> 您应该提供两个对象 sender 和 e (它们将在稍后得到类型推断)。

backgroundWorker1.DoWork += (sender, e) => ...
于 2010-01-16T15:15:02.847 回答
4

让我们变得简单

Lambda 表达式非常方便,可以使代码更短且更具可读性。然而,入门级程序员可能会发现它有点难以处理。应该了解三个独立的概念:匿名方法、委托和 lambda 表达式。他们每个人的详细演练超出了这个答案的范围。我希望下面给出的代码示例将有助于快速查看可用的不同方法。

class TestBed
{
    BackgroundWorker bgw = new BackgroundWorker();
    void sample()
    {            
        //approach #1
        bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
        //DoWorkEventHandler is nothing but a readily available delegate written by smart Microsoft guys

        //approach #2, to make it a little shorter
        bgw.DoWork += (s,e) => 
        {
            //...
        };
        //this is called lambda expression (see the => symbol)

        //approach #3, if lambda scares you
        bgw.DoWork += delegate 
        { 
            //... (but you can't have parameters in this approach
        };

        //approach #4, have a helper method to prepare the background worker
        prepareBgw((s,e)=>
        {
            //...
        }
        );

        //approach #5, helper along with a simple delegate, but no params possible
        prepareBgw(delegate 
        {
            //...
        }
        );

        //approach #6, helper along with passing the methodname as a delegate
        prepareBgw(bgw_DoWork);

        //approach #7, helper method applied on approach #1
        prepareBgw(new DoWorkEventHandler(bgw_DoWork));

    }

    void bgw_DoWork(object sender, DoWorkEventArgs e)
    {
        //...
    }
    void prepareBgw(DoWorkEventHandler doWork)
    {
        bgw.DoWork+= doWork;
    }
}

请注意,我们在此示例中使用了“delegate”而不是“Delegate”(两者之间存在差异)

于 2014-04-18T09:24:54.897 回答