private static void LoadUsers(IEnumerable<long> users,
object lockObject,
IDictionary<long, TwitterUser> dictionary,
IList<TwitterUser> target)
{
var source = users.Reverse().ToArray();
Task.Run(async () =>
{
var loadSource = new HashSet<long>();
lock (lockObject)
{
foreach (var userId in source)
{
// check dictionary not contains the id
if (dictionary.ContainsKey(userId)) continue;
// acquire position
dictionary.Add(userId, null);
loadSource.Add(userId);
}
}
var ud = (await StoreHelper.GetUsersAsync(loadSource).ConfigureAwait(false)).ToDictionary(u => u.Id);
lock (lockObject)
{
foreach (var userId in source)
{
TwitterUser user;
if (!dictionary.TryGetValue(userId, out user) || user != null)
{
// user is not in dictionary or
// user is already loaded => skip adding
continue;
}
var nu = ud[userId];
dictionary[userId] = nu;
target.Insert(0, nu);
}
}
});
}