BackgroundWorker with anonymous methods?

前端 未结 4 1389
猫巷女王i
猫巷女王i 2021-02-05 03:55

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

BackgroundWorker bgw = new B         


        
4条回答
  •  深忆病人
    2021-02-05 04:11

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

提交回复
热议问题