28

匿名メソッドでBackgroundWorkerを作成します。
私は次のコードを書きました:

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


しかし、デリゲート 'System.ComponentModel.DoWorkEventHandler' は '0' 引数を取らず、匿名メソッドに 2 つのオブジェクトを渡す必要があります:オブジェクト送信者、DoWorkEventArgs e

どうすればそれを行うことができますか?ありがとう。

4

4 に答える 4

57

匿名関数にパラメーターを追加するだけです。

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

または、パラメーターを気にしない場合は、次のことができます。

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

ラムダを指定する場合は、同じ数の引数を取るようにする必要があります。

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

ただし、引数を使用しない場合は、パラメーターなしで匿名デリゲートを使用できます。

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

ラムダなしで上記を書いた場合、それはどうなるでしょうか?

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

しかし、今ではバインドされた変数なしでラムダを使用しています ()=> 2 つのオブジェクト sender と e を提供する必要があります (これらは後で型が推定されます)。

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

シンプルにしましょう

ラムダ式は、コードを短くして読みやすくするのに非常に便利です。ただし、初心者レベルのプログラマーは、対処するのが少し難しいと感じるかもしれません。通過する必要がある 3 つの別個の概念があります: 匿名メソッド、デリゲート、およびラムダ式です。それぞれの詳細なウォークスルーは、この回答の範囲を超えています。以下に示すコード例が、利用可能なさまざまなアプローチの概要を示す目的に役立つことを願っています。

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

この例では、「デリゲート」ではなく「デリゲート」を使用していることに注意してください (2 つには違いがあります)。

于 2014-04-18T09:24:54.897 に答える