Himanshu.Parkash
Himanshu.Parkash

Reputation: 153

Parallel task, while using ContinueWith, result of first action is not finding

I am new to TPL , so just going through some of Tutorials of regarding the same , http://msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx

Here in above Link i am trying to Use This Code

 var displayData = Task.Factory.StartNew(() => { 
                                         Random rnd = new Random(); 
                                         int[] values = new int[100];
                                         for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
                                            values[ctr] = rnd.Next();

                                         return values;
                                      } ).  
                ContinueWith((x) => {
                                int n = x.Result.Length;
                                long sum = 0;
                                double mean;

                                for (int ctr = 0; ctr <= x.Result.GetUpperBound(0); ctr++)
                                   sum += x.Result[ctr];

                                mean = sum / (double) n;
                                return Tuple.Create(n, sum, mean);
                             } ). 
                ContinueWith((x) => {
                                return String.Format("N={0:N0}, Total = {1:N0}, Mean = {2:N2}",
                                                     x.Result.Item1, x.Result.Item2, 
                                                     x.Result.Item3);
                             } );                         

I am Replacing above code With Mine

 var taskList = Task.Factory.StartNew(() => {
                var newRandom = new Random();
                var intArray = new int[100];
                Console.WriteLine("Begining  of First  Iteration");
                Parallel.For(0, intArray.GetUpperBound(0) - 1, i =>
                {
                    intArray[i] = newRandom.Next();
                    Console.WriteLine(i);
                });
                Console.WriteLine("End of First Iteration");

            }).ContinueWith((x) => {
                Console.WriteLine("Beginning of Second  Iteration");
                int n = x.Result.Length;
                long sum = 0;
                double mean;

                for (int ctr = 0; ctr <= x.Result.GetUpperBound(0); ctr++)
                    sum += x.Result[ctr];

                mean = sum / (double)n;
                Console.WriteLine("End of  Second Iteration");
                return Tuple.Create(n, sum, mean);
                });

I am getting the Error ::

'System.Threading.Tasks.Task' does not contain a definition for 'Result' and no extension method 'Result' accepting a first argument of type 'System.Threading.Tasks.Task' could be found (are you missing a using directive or an assembly reference?) 

It not finding the "Result"

What i am doing wrong ??

Upvotes: 1

Views: 2319

Answers (1)

Eren Ers&#246;nmez
Eren Ers&#246;nmez

Reputation: 39085

Task class does not have a Result property, Task<T> does.

The problem is, your delegate in the StartNew does not return a value, so it resolves to returning a Task instead of Task<T>.

Try adding this:

...
Console.WriteLine("End of First Iteration");
return intArray;

Upvotes: 4

Related Questions