传递函数作为参数来创建委托

时间:2015-03-10 02:22:54

标签: c#

我正在尝试制作辅助函数来制作BackgroundWorkers。

这是我到目前为止所拥有的。

using System.ComponentModel;
using System;

public class BackgroundThread {

    BackgroundWorker worker;

    public BackgroundThread(Delegate workerFunction, Delegate workerCallback) {
        this.worker = new BackgroundWorker();
        this.worker.DoWork += new DoWorkEventHandler(workerFunction);
        this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(workerCallback);
    }

    public void Start(object argument) {
        this.worker.RunWorkerAsync(argument);
    }
}

虽然我收到了这个错误。

Expression denotes a 'variable', where a 'type' or 'method group' was expected

这是有道理的,因为通常您将对函数的引用传递给处理程序,但我不知道如何在此上下文中执行此操作。或者这是不可能的。我不太了解C#代表,知道该怎么做。

2 个答案:

答案 0 :(得分:3)

喜欢这个吗?

public class BackgroundThread
{

    System.ComponentModel.BackgroundWorker worker;

    public BackgroundThread(System.ComponentModel.DoWorkEventHandler workerFunction, System.ComponentModel.RunWorkerCompletedEventHandler workerCallback)
    {
        this.worker = new System.ComponentModel.BackgroundWorker();
        this.worker.DoWork += workerFunction;
        this.worker.RunWorkerCompleted += workerCallback;
    }

    public BackgroundThread(Action<object> anyWorkFunctionWithObjectArgument, Action<object> anyCallback)
    {
        this.worker = new System.ComponentModel.BackgroundWorker();
        this.worker.DoWork += (sender, e) => { anyWorkFunctionWithObjectArgument.Invoke(e.Argument); };
        this.worker.RunWorkerCompleted += (sender, e) => { anyCallback.Invoke(e.Result); };
    }

    public void Start(object argument)
    {
        this.worker.RunWorkerAsync(argument);
    }

    public static BackgroundThread GetDoNothingInstance()
    {
        return new BackgroundThread(
            (sender, e) =>
            {
                // e is DoWorkEventArgs
            },
            (sender, e) =>
            {
                // e is RunWorkerCompletedEventArgs
            });
    }

    public static BackgroundThread GetDoNothingInstance2()
    {
        Action<object> workfunction = delegate(object argument)
        {
            // Do nothing
        };

        Action<object> callback = delegate(object result) 
        { 
            // Do nothing
        };

        return new BackgroundThread(workfunction, callback);       
    }
}

答案 1 :(得分:2)

刚看到你的评论。这应该允许你只传递一个简单的旧功能&#34;无需像处理程序那样塑造它:

class Program
{
    protected static void plainOldWorkerFunction(object argument)
    {
        return;
    }

    protected static void plainOldCallbackFunction()
    {
        return;
    }

    static void Main(string[] args)
    {
        BackgroundThread bt = new BackgroundThread(plainOldWorkerFunction, plainOldCallbackFunction);

        bt.Start(1234);
    }
}

public class BackgroundThread
{
    BackgroundWorker worker;
    Action<object> workerAction;
    Action callbackAction;

    protected void doWork(object sender, DoWorkEventArgs e)
    {
        workerAction(e.Argument);
    }

    protected void callback(object sender, RunWorkerCompletedEventArgs e)
    {
        callbackAction();
    }

    public BackgroundThread(Action<object> workerFunction, Action workerCallback)
    {
        this.workerAction = workerFunction;
        this.callbackAction = workerCallback;

        this.worker = new BackgroundWorker();
        this.worker.DoWork += doWork;
        this.worker.RunWorkerCompleted += callback;
    }

    public void Start(object argument)
    {
        this.worker.RunWorkerAsync(argument);
    }
}

原始答案:

请尝试使用此构造函数:

public BackgroundThread(DoWorkEventHandler workerFunction, RunWorkerCompletedEventHandler workerCallback)
{
    this.worker = new BackgroundWorker();
    this.worker.DoWork += workerFunction;
    this.worker.RunWorkerCompleted += workerCallback;
}

并确保您的workerFunction和workerCallback具有以下参数:

protected static void workerFunction (object sender, DoWorkEventArgs e)
{
    return;
}

protected static void workerCallback (object sender, RunWorkerCompletedEventArgs e)
{
    return;
}