5

I am attempting to get the return data from my task, it works ok if i use a single var, but when i use an array or arraylist, i do not see the interface for RESULT in the available properties methods of the task object.

var task = Task<BookingListResponse>
           .Factory.StartNew(() => GetServicesFromApi(sc),
                             TaskCreationOptions.LongRunning);
tasks.Add(task);
try
{
   // Wait for all the tasks to finish.
   Task.WaitAll(tasks.ToArray());
}

as you can see from the code, if i put the tasks back into an array and type tasks[1].Result, it does not expose 'result', if i access task then i can get it.

I am sure i am doing something silly, so any help would be good.

cheers.

Paul.


here is the full code:

List<Task> tasks = new List<Task>();

// loop schemes and only call DISTINCT transit api URL's
foreach (Scheme scheme in schemes)
{
   if (url.ContainsKey(scheme.Url))
      continue;

   url.Add(scheme.Url, 0); // add url.

   var sc = new ServiceCriteria();
   sc.Url = scheme.Url;
   sc.CapacityRequirement = capacityRequirement;
   sc.DropOffLocation = dropOffLocation;
   sc.PickUpLocation = pickUpLocation;
   sc.PickUp = pickup;
   sc.TravelTime = travelTime;

   // Fire off thread for each method call.
   //tasks.Add(Task<BookingListResponse>.Factory.StartNew(apiAttributes =>
   //            GetServicesFromApi(sc), TaskCreationOptions.LongRunning));

   var task = Task<BookingListResponse>
                 .Factory.StartNew(() => GetServicesFromApi(sc), 
                                   TaskCreationOptions.LongRunning);
   tasks.Add(task);

}


try
{
   // Wait for all the tasks to finish.
   Task.WaitAll(tasks.ToArray());
   var result = tasks[0].Result;
}

the result option does not show.

cheers.

3
  • 1
    We can't really see that, you have not pasted all relevant code. 1) declaration of tasks is missing. 2) Where you try to read the result is also missing. Commented Dec 6, 2010 at 13:22
  • 1
    Where's the definition of tasks? Are you ending up with an array of Task instead of an array of Task<BooklingListResult>? Commented Dec 6, 2010 at 13:24
  • yes, i am ending up with an array of tasks and attempting to get the result from the tasks, if i use an array of my BO then i get the values, but i am unsure if that is correct, could i still use the Task.WaitAll and the array of BooklingListResult will be populated? Commented Dec 6, 2010 at 13:30

2 Answers 2

10

You need to cast your list of Tasks into Task<BookingListResponse>...

So do:

var result = ((Task<BookingListResponse>)tasks[0]).Result;
Sign up to request clarification or add additional context in comments.

1 Comment

Even better: why not declare tasks as var tasks = new List<Task<BookingListResponse>>(); ?
-1
task.Result

or

tasks.First().Result

should work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.