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
Thank you,
Kind regards
Triacona
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:
[COLOR=#3465A4]private static void[/color] TaskWaitAny()
{
[COLOR=#4E9A06]// Task [array] <of return type integer> or direct as Task<of return type integer>[that is an array of tasks][/color]
[COLOR=#3465A4]Task[/color]<[COLOR=#3465A4]int[/color]>[] tasks = [COLOR=#3465A4]new[/color] [COLOR=#3465A4]Task[/color]<[COLOR=#3465A4]int[/color]>[3];
tasks[0] = Task.Run(() => { Thread.Sleep(2000); [COLOR=#3465A4]return[/color] 1; });
tasks[1] = Task.Run(() => { Thread.Sleep(2000); [COLOR=#3465A4]return[/color] 2; });
tasks[2] = Task.Run(() => { Thread.Sleep(2000); [COLOR=#3465A4]return[/color] 3; });
while (tasks.Length > 0)
{
[COLOR=#3465A4]int[/color] i = Task.WaitAny(tasks); // WaitAny(): Returns: The index of the completed task in the tasks array argument.
[COLOR=#3465A4]int[/color] y = i;
[COLOR=#3465A4]Task[/color]<[COLOR=#3465A4]int[/color]> completedTask = tasks[i];
Console.WriteLine([COLOR=#CE5C00]"completedTask.Result: {0} same as tasks[i].Result: {1} and y (current i val) is: {2} "[/color], completedTask.Result, tasks[i].Result, y);
[COLOR=#4E9A06] // 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[/color]
[COLOR=#3465A4]List[/color]<[COLOR=#3465A4]Task[/color]<[COLOR=#3465A4]int[/color]>> temp = tasks.ToList();
temp.RemoveAt(i);
tasks = temp.ToArray();
}
}
Thank you,
Kind regards
Triacona