[英]Can you use pre-defined Action<T> in Control.BeginInvoke
我有一些線程檢查,我可以通過調用BeginInvoke(new Action<string,string>........
得到以下工作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;
}
}
這是你的意思嗎?
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);
}
如果我理解正確你想要這樣的東西?
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
}
}
您必須將普通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
}
}
這條路?:
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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.