private static async Task <int[][][]> VoerUitMetReturn(string type) { int[] dimensies = { 1000, 500, 100, 800, 700, 300 }; //willekeurige volgorde List <Task <int[][]> > tasks = new List <Task <int[][]> >(); //asynchroon opstarten van de matrixberekeningen. for (int i = 0; i < dimensies.Length; i++) { int dim = dimensies[i]; int[,] a = MatrixOperations.CreateMatrix(dim); int[,] b = MatrixOperations.CreateMatrix(dim); Console.WriteLine("\t" + type + ": gestart: " + dim); tasks.Add(MatrixOperations.MatrixProductAsync(a, b, methods[type])); } int aantal = tasks.Count; while (tasks.Count > 0) { // When any await the first task to finish and returns it. Task <int[][]> finishedTask = await Task.WhenAny(tasks); // We can now await the completed task without waiting as we know it has already finished. int[][] product = await finishedTask; Console.WriteLine("\t" + type + ": Afgewerkt:" + product.GetLength(0) + "x" + product[0].GetLength(0)); // Remove the finished task from the list so that you don't process it more than once. tasks.Remove(finishedTask); } var allTasks = await Task.WhenAll(tasks); return(allTasks); }
private static async Task VoerUit2Async(string type) { int[] dimensies = { 1000, 500, 100, 800, 700, 300 }; // willkeurige volgorde List <Task <int[][]> > tasks = new List <Task <int[][]> >(); for (int i = 0; i < dimensies.Length; i++) { int dim = dimensies[i]; int[,] a = MatrixOperations.CreateMatrix(dim); int[,] b = MatrixOperations.CreateMatrix(dim); Console.WriteLine("\tGestart: " + dim); tasks.Add(MatrixOperations.MatrixProductAsync(a, b, methods[type])); } Console.WriteLine("aanal tasks= " + tasks.Count); while (tasks.Count > 0) { //eerste task die klaar is Task <int[][]> firstFinishedTask = await Task.WhenAny(tasks); int[][] product = firstFinishedTask.Result; Console.Write("\t" + type + " afgewerkt: " + product.GetLength(0) + "x" + product[product.GetLength(0) - 1].GetLength(0)); //taak verwijderen tasks.Remove(firstFinishedTask); Console.WriteLine(" Resterende tasks= " + tasks.Count); } }
private static void VoerUit(string type) { List <Task <int[][]> > tasks = new List <Task <int[][]> >(); //asynchroon opstarten van de matrixberekeningen. for (int dim = 100; dim <= MAXDIM; dim = dim + STEP) { int[,] a = MatrixOperations.CreateMatrix(dim); int[,] b = MatrixOperations.CreateMatrix(dim); Console.WriteLine("\tGestart: " + dim); tasks.Add(MatrixOperations.MatrixProductAsync(a, b, methods[type])); } foreach (Task <int[][]> t in tasks) { int[][] product = t.Result; Console.WriteLine("\tAfgewerkt: " + product.GetLength(0) + "x" + product[product.GetLength(0) - 1].GetLength(0)); } Console.Write(tasks.Count + " taksks afgewerkt"); }