在下面的代码示例中,如何在回调方法中获取输入参数内容" 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
}
}
答案 0 :(得分:1)
你可以这样写,并使用任何东西:
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);
}
答案 1 :(得分:0)
来自MSDN:
public delegate void MyDelegate(Label myControl, string myArg2);
private void Button_Click(object sender, EventArgs e)
{
object[] myArray = new object[2];
myArray[0] = new Label();
myArray[1] = "Enter a Value";
myTextBox.BeginInvoke(new MyDelegate(DelegateMethod), myArray);
}
public void DelegateMethod(Label myControl, string myCaption)
{
myControl.Location = new Point(16,16);
myControl.Size = new Size(80, 25);
myControl.Text = myCaption;
this.Controls.Add(myControl);
}
以下是该文章的进一步阅读链接:http://msdn.microsoft.com/en-us/library/a06c0dc2(v=vs.110).aspx