我有一些线程检查,我可以通过调用BeginInvoke(new Action<string,string>........
来完成下面的工作,但我想知道你是否可以某种方式使用预定义的动作?
private Action<string, string> DoSomething();
private void MakeItHappen(string InputA, string InputB)
{
if (this.InvokeRequired)
{
this.BeginInvoke(DoSomething(InputA, InputB));
}
else
{
Label.Text = "Done";
MyOtherGUIItem.Visible = false;
}
}
答案 0 :(得分:1)
这是你的意思吗?
private Action<string, string> _DoSomething;
public void ConfigureToSecondThanFirstByExistingFunction()
{
_DoSomething = MyMakeItLowerImplementation;
}
public void ConfigureToFirstThenSecondByLambda()
{
_DoSomething = (a, b) => Console.WriteLine(a + b);
}
public void CallMe()
{
ConfigureToSecondThanFirstByExistingFunction();
//ConfigureToFirstThenSecondByLambda();
_DoSomething("first", "second");
}
private void MyMakeItLowerImplementation(string a, string b)
{
Console.WriteLine(b + a);
}
答案 1 :(得分:1)
如果我理解正确你想要这样的东西吗?
private delegate void updateDelegate(string p1, string p2);
private updateDelegate DoSomething;
private void MakeItHappen(string InputA, string InputB)
{
if (this.InvokeRequired)
{
this.BeginInvoke(DoSomething, InputA, InputB);
}
else
{
//Do stuff
}
}
答案 2 :(得分:0)
您必须将普通delegate
传递给BeginInvoke
。您可以使用变量捕获来包装您的参数,如下所示:
private void MakeItHappen(string InputA, string InputB)
{
if (this.InvokeRequired)
{
this.BeginInvoke((Action)delegate () {
MakeItHappen(InputA, InputB);
});
}
else
{
// do stuff with InputA and InputB
}
}
答案 3 :(得分:0)
这样?:
private delegate void DoSomethingHandler(string A, string B);
private event DoSomethingHandler DoSomething;
public void Start()
{
DoSomething = (a, b) => { /*doStuff*/ };
}
private void MakeItHappen(string InputA, string InputB)
{
if (this.InvokeRequired)
{
this.BeginInvoke(DoSomething, InputA, InputB);
}
else
{
//Do stuff
}
}