我有BeginInvoke
一个代表,internally invokes multiple asynchronous operations
我想等待所有内部操作完成before callback for main asynchronous operation executes.
我可以很容易地做到这一点,async, await or TPL
但因为我的目标平台是.Net3.5
. 演示我的问题的示例代码 -
class Program
{
static List<string> abc = new List<string>();
static void Main(string[] args)
{
new Action(() =>
{
A();
}).BeginInvoke(MainCallback, null);
}
static void MainCallback(IAsyncResult result)
{
foreach (string str in abc)
{
Console.WriteLine(String.Format("Main Callback {0}",str));
}
}
static void A()
{
for (int i = 0; i < 10; i++)
{
new Action(() =>
{
Thread.Sleep(1000);
}).BeginInvoke(Callback, i);
}
}
static void Callback(IAsyncResult result)
{
abc.Add(result.AsyncState.ToString());
}
}
我希望输出是这样的 -
Main Callback 0
Main Callback 1
Main Callback 2
Main Callback 3
Main Callback 4
Main Callback 5
Main Callback 6
Main Callback 7
Main Callback 8
Main Callback 9