【问题标题】:BackgroundWorker with anonymous methods?具有匿名方法的BackgroundWorker?
【发布时间】:2011-01-05 20:56:34
【问题描述】:

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

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


Delegate 'System.ComponentModel.DoWorkEventHandler' 不接受 '0' 参数,我必须将两个对象传递给匿名方法:object sender , DoWorkEventArgs e

请指导我,我该怎么做? 谢谢。

【问题讨论】:

    标签: c# .net backgroundworker anonymous-methods


    【解决方案1】:

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

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

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

    bgw.DoWork += delegate { ... }
    

    【讨论】:

    • @Jader:经过编辑,相当于我的答案。为什么不直接投票给我的答案?
    【解决方案2】:

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

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

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

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

    【讨论】:

    • @Offler:如果您使用了 lambda 或匿名委托,则不能使用 -=。您可以先在局部变量中捕获它,然后在 +=-= 中使用它。
    【解决方案3】:

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

    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) => ...
    

    【讨论】:

      【解决方案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”(两者有区别)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-15
        • 2020-06-06
        • 1970-01-01
        • 2012-12-29
        • 1970-01-01
        相关资源
        最近更新 更多