I have BeginInvoke a delegate which internally invokes multiple asynchronous operations and i want to wait for all internal operations to complete before callback for main asynchronous operation executes.
I could have easily achieved that using async, await or TPL but can't since my target platform is .Net3.5. Sample code demonstrating my problem -
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()); } } I want the output to be something like this -
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