Why can't I display the index in this task array
Why can't I display the index in this task array
(OP)
Hi all, 
I'm a bit stuck here in terms of how to display the index number for debugging...
I want to display the value of each i ( index ) so I can see what the index value is at any point in the loop.
It seems to work for the first value each time, but then gives 0 for all others..
Please help, this will give me better understanding of how the WaitAll works...please see code below.
Thanks

I'm a bit stuck here in terms of how to display the index number for debugging...
I want to display the value of each i ( index ) so I can see what the index value is at any point in the loop.
It seems to work for the first value each time, but then gives 0 for all others..
Please help, this will give me better understanding of how the WaitAll works...please see code below.
Thanks

CODE --> c#
private static void TaskWaitAny() { // Task [array] <of return type integer> or direct as Task<of return type integer>[that is an array of tasks] Task<int>[] tasks = new Task<int>[3]; tasks[0] = Task.Run(() => { Thread.Sleep(2000); return 1; }); tasks[1] = Task.Run(() => { Thread.Sleep(2000); return 2; }); tasks[2] = Task.Run(() => { Thread.Sleep(2000); return 3; }); while (tasks.Length > 0) { int i = Task.WaitAny(tasks); // WaitAny(): Returns: The index of the completed task in the tasks array argument. int y = i; Task<int> completedTask = tasks[i]; Console.WriteLine("completedTask.Result: {0} same as tasks[i].Result: {1} and y (current i val) is: {2} ", completedTask.Result, tasks[i].Result, y); // code below creates a list of the tasks, then removes an entry at i // then overwrites the tasks array with the new array with one less item //.'. list gets smaller each time and no inifinite loop List<Task<int>> temp = tasks.ToList(); temp.RemoveAt(i); tasks = temp.ToArray(); } }
Thank you,
Kind regards
Triacona
RE: Why can't I display the index in this task array
CODE