×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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, 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 --> 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

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; }); 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close