Reputation: 12659
I have a facebook query which is as follows
var client = new FacebookClient(accessToken);
var dynamic = client.Get(connection, new { fields = "name,from,story,message,picture,comments", limit = count});
foreach (var dynPost in results.data)
{
posts.Add(ConvertToPost(dynPost));
}
I am trying to make this quicker by making lots of smaller requests to Facebook in parallel.
using (profilingService.Start("Facebook calls"))
{
var pullSize = 25;
var numberOfCalls = (int)Math.Ceiling(count / (double)pullSize);
var taskQueue = new Queue<Task>();
for (int i = 0; i < numberOfCalls; i++)
{
taskQueue.Enqueue(Task.Factory.StartNew(() =>
{
var offset = i * pullSize;
var client = new FacebookClient(accessToken);
return client.Get(connection, new { fields = "name,from,story,message,picture,comments", limit = 25, offset = offset });
}));
}
Task.Factory.ContinueWhenAll(
taskQueue,
tasks =>
{
//t.Result here is giving me the error
var results = tasks.Select(t => t.Result);
foreach (var result in results)
{
foreach (var dynPost in result.data)
{
posts.Add(ConvertToPost(dynPost.data));
}
}
});
}
First of all I am getting an error on var results = tasks.Select(t => t.Result);
Error 361 '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?)
t is of type Task<AntecedentResult>
Which led me to this page, but have still not able to resolve it. What's wrong with this async Task method?
Also, I haven't had much experience with this sort of parallel coding, anything I am doing wrong or should be doing in a different way?
Upvotes: 2
Views: 8935
Reputation: 6932
The problem is the Queue<Task>
. Task
alone does not have a Result
property. However, Task<Foo>
has a Result
property of type Foo
. (I don't know what client.Get(...)
returns, let's call it Foo
for now:
var taskQueue = new Queue<Task<Foo>>();
The ContinueWhenAll
method does not support Queue<>
, only Task[]
and Task<>[]
, so you need to convert it to an array first.
Task.Factory.ContinueWhenAll(
taskQueue.ToArray(),
tasks =>
{
var results = tasks.Select(t => t.Result);
foreach (var result in results)
{
...
}
});
}
Upvotes: 6