C# 将函数作为参数传递以创建委托
我正在尝试创建一个助手函数来创建后台工作人员 这是我到目前为止所拥有的C# 将函数作为参数传递以创建委托,c#,C#,我正在尝试创建一个助手函数来创建后台工作人员 这是我到目前为止所拥有的 using System.ComponentModel; using System; public class BackgroundThread { BackgroundWorker worker; public BackgroundThread(Delegate workerFunction, Delegate workerCallback) { this.worker = new B
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);
}
}
虽然我得到了这个错误
表达式表示“变量”,其中需要“类型”或“方法组”
这是有意义的,因为通常您会将函数的引用传递给处理程序,但我不知道在这种情况下如何执行。或者这是不可能的。我对C#代表之类的人了解不够,不知道该怎么办。刚刚看到你的评论。这应该允许您只传递一个“普通的旧函数”,而不必像处理程序一样对其进行造型:
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);
}
}
只需确保workerFunction和workerCallback具有以下参数:
protected static void workerFunction (object sender, DoWorkEventArgs e)
{
return;
}
protected static void workerCallback (object sender, RunWorkerCompletedEventArgs e)
{
return;
}
像这样
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);
}
}
公共类背景线程
{
System.ComponentModel.BackgroundWorker工人;
公共后台线程(System.ComponentModel.DoWorkEventHandler workerFunction、System.ComponentModel.RunWorkerCompleteDevenHandler workerCallback)
{
this.worker=new System.ComponentModel.BackgroundWorker();
this.worker.DoWork+=workerFunction;
this.worker.RunWorkerCompleted+=workerCallback;
}
public BackgroundThread(Action AnyWorkFunction with ObjectArgument,Action anyCallback)
{
this.worker=new System.ComponentModel.BackgroundWorker();
this.worker.DoWork+=(发送方,e)=>{anyWorkFunctionWithObjectArgument.Invoke(e.Argument);};
this.worker.RunWorkerCompleted+=(发送方,e)=>{anyCallback.Invoke(e.Result);};
}
公共void开始(对象参数)
{
this.worker.RunWorkerAsync(参数);
}
公共静态背景线程GetDoNothingInstance()
{
返回新的背景线程(
(发件人,e)=>
{
//e是DowereVentargs
},
(发件人,e)=>
{
//e是RunWorkerCompletedEventArgs
});
}
公共静态背景线程GetDoNothingInstance2()
{
Action workfunction=委托(对象参数)
{
//无所事事
};
动作回调=委托(对象结果)
{
//无所事事
};
返回新的后台线程(workfunction,callback);
}
}
我确实想到了这个,就像萨利的一样。但在我的例子中,这需要在BackgroundThread类之外实例化处理程序。如果这是必须要做的事情,那就这样吧。我只是希望有一种方法可以在构造函数中实现。你们两个都使用相同的方法,但我更喜欢这个方法,因为它将所有内容都保存在构造函数中。谢谢你的帮助。我也更喜欢这个:)我知道可能有更简洁的方法来写这个。干得好,埃里克。
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);
}
}