2010-01-16 56 views
25

我要用匿名方法创建一个BackgroundWorker
我已经写了下面的代码:

带有匿名方法的BackgroundWorker?

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


委托 'System.ComponentModel.DoWorkEventHandler' 不采取 '0' 的论点,我必须通过两项对象到匿名方法:对象发件人,DoWorkEventArgs e

Coul你请引导我,我怎么能做到这一点? 谢谢。

回答

48

你只需要参数添加到匿名函数:

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

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

bgw.DoWork += delegate { ... } 
+0

@Jader:编辑,以便它是我的回答是等价的。为什么不直接投我的答案? – 2010-01-16 18:05:17

30

如果指定拉姆达你必须确保它需要相同数量的参数:

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

但是,如果您不使用参数, t使用不带参数的匿名代理:

bgw.DoWork += delegate 
{ 
    ... 
}; 
+0

我不知道你会做什么惠特“ - =”... – Offler 2013-03-25 15:20:45

+0

@Offler:如果你使用了lambda或匿名委托,你不能使用' - ='。你可以首先在局部变量中捕获它,并在'+ ='和' - ='中使用它。 – 2013-03-25 20:16:51

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); 
    } 

但是现在你正在使用的lambda表达式没有绑定变量()=> 你应该提供两个对象sender和e(这是他们将获得型后推断)。

backgroundWorker1.DoWork += (sender, e) => ... 
2

让使简单

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; 
    } 
} 

注(在这两个之间的差异)