public static List <Pais> ContinueWhenAll() { // Schedule a list of tasks that return integer Task <string>[] tasks = new Task <string>[] { Task <string> .Factory.StartNew(() => { Thread.Sleep(500); Console.WriteLine("Task={0}, Thread={1}, x=5", Task.CurrentId, Thread.CurrentThread.ManagedThreadId); return(Metodos.ObtenerNombrePaisAsyncContentString().Result); }), Task <string> .Factory.StartNew(() => { Thread.Sleep(10); Console.WriteLine("Task={0}, Thread={1}, x=3", Task.CurrentId, Thread.CurrentThread.ManagedThreadId); return(Metodos.ObtenerPaisAsyncContentString().Result); }), Task <string> .Factory.StartNew(() => { Thread.Sleep(20000); Console.WriteLine("Task={0}, Thread={1}, x=2", Task.CurrentId, Thread.CurrentThread.ManagedThreadId); return(Metodos.ObtenerPaisAsyncContentString().Result); }) }; // Schedule a continuation to indicate the result of the first task to complete //Task.Factory.ContinueWhenAny(tasks, winner => //{ // // You would expect winning result = 3 on multi-core systems, because you expect // // tasks[1] to finish first. // Console.WriteLine("Task={0}, Thread={1} (ContinueWhenAny): Winning result = {2}", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, winner.Result); //}); var lista = new List <Pais>(); // Schedule a continuation that sums up the results of all tasks, then wait on it. // The list of antecendent tasks is passed as an argument by the runtime. Task.Factory.ContinueWhenAll(tasks, (antecendents) => { int sum = 0; foreach (Task <string> task in antecendents) { lista.AddRange(JsonConvert.DeserializeObject <List <Pais> >(task.Result)); } Console.WriteLine("Task={0}, Thread={1}, (ContinueWhenAll): Total={2} (expected 10)", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, sum); }) .Wait(); return(lista); }
public static async Task Inicio() { WaitAnyTasks[0] = (Task.Run(() => { Metodos.ObtenerPaisAsyncContentString(); })); WaitAnyTasks[1] = (Task.Run(() => { Metodos.ObtenerNombrePaisAsyncContentString(); })); WhenAnyTasks[0] = (Task.Run(() => { Metodos.ObtenerPaisAsyncContentString(); })); WhenAnyTasks[1] = (Task.Run(() => { Metodos.ObtenerNombrePaisAsyncContentString(); })); WaitAllTasks[0] = (Task.Run(() => { Metodos.ObtenerPaisAsyncContentString(); })); WaitAllTasks[1] = (Task.Run(() => { Metodos.ObtenerNombrePaisAsyncContentString(); })); WhenAllTasks[0] = (Task.Run(() => { Metodos.ObtenerPaisAsyncContentString(); })); WhenAllTasks[1] = (Task.Run(() => { Metodos.ObtenerNombrePaisAsyncContentString(); })); ClaseListaPaises.AdicionarPais(new Pais_0()); ClaseListaPaises.AdicionarPais(new Pais_1()); Console.WriteLine("Tareas cargadas..."); Console.WriteLine("Ejecutando proceso..."); }