带有匿名方法的BackgroundWorker?

发布于 2024-08-17 17:14:19 字数 511 浏览 9 评论 0原文

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

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


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

您能否指导我,我该怎么做? 谢谢。

I'm gonna create a BackgroundWorker with an anonymous method.
I've written the following code :

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

But Delegate 'System.ComponentModel.DoWorkEventHandler' does not take '0' arguments and I have to pass two objects to the anonymous method : object sender, DoWorkEventArgs e

Could you please guide me, how I can do it ?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南城追梦 2024-08-24 17:14:19

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

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

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

bgw.DoWork += delegate { ... }

You just need to add parameters to the anonymous function:

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

Or if you don't care about the parameters you can just:

bgw.DoWork += delegate { ... }
冷情妓 2024-08-24 17:14:19

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

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

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

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

If you specify a lambda, you must ensure it takes the same number of arguments:

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

But if you're not using the arguments, you could just use an anonymous delegate without parameters:

bgw.DoWork += delegate
{
    ...
};
满天都是小星星 2024-08-24 17:14:19

如果你在没有 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) => ...

If you have written the above without lambdas how it would be?

backgroundWorker1.DoWork += 
                new DoWorkEventHandler(backgroundWorker1_DoWork);

and the named method:

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

But now you are using lambdas with no bound variables ()=>
You should provide two objects sender and e (which they will get type inferred later).

backgroundWorker1.DoWork += (sender, e) => ...
峩卟喜欢 2024-08-24 17:14:19

让我们变得简单

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”(两者之间是有区别的)

Lets make it simple

Lambda expressions are really handy to make the code shorter and more readable. However entry level programmers might find it a bit difficult to deal with. There are three separate concepts one should go through: anonymous methods, delegates and lambda expressions. A detailed walk-through of each of them is beyond the scope of this answer. I hope that the code example given below will serve the purpose of giving a quick view of the different approaches available.

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

Note that we used "delegate" and not "Delegate" in this example (there is a difference between the two)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文