Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why can't I display the index in this task array 1

Status
Not open for further replies.

Triacona

Technical User
Jun 11, 2009
462
GB
Hi all, [smile]
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 [bigsmile]
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
 
It waits 2 seconds for the first task to finish. By that time, the others have also finished so they exit. Try
Code:
            tasks[0] = Task.Run(() => { Thread.Sleep(2000); return 1; });
            tasks[1] = Task.Run(() => { Thread.Sleep(4000); return 2; });
            tasks[2] = Task.Run(() => { Thread.Sleep(6000); return 3; });
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top