gpt4 book ai didi

c# - 在回调方法中获取输入参数

转载 作者:太空宇宙 更新时间:2023-11-03 13:20:56 25 4
gpt4 key购买 nike

在下面的代码示例中,如何在回调方法“MethodDone”中获取输入参数内容?

我不想再次将输入参数作为 BeginInvoke 的第三个参数传递,因为我想在回调方法中调用 EndInvoke。

static class Program
{
static void Main()
{
Action<string> del = new Action<string>(SomeMethod);
del.BeginInvoke("input parameter", MethodDone, del);
}

static void MethodDone(IAsyncResult ar)
{
//how to get input parameter here ?

Action<string> del = (Action<string>)ar.AsyncState;
del.EndInvoke(ar);
}

static void SomeMethod(string input)
{
//do something
}
}

最佳答案

你可以这样写并使用任何东西:

static void Main()
{
string myInput = "Test";
Action<string> del = new Action<string>(SomeMethod);
del.BeginInvoke(
"input parameter",
(IAsyncResult ar) =>
{
Console.WriteLine("More Input parameters..." + myInput);
del.EndInvoke(ar);
},
del);
}

关于c# - 在回调方法中获取输入参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24244205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com