In a windows store app project i have a function that i call several times inside a foreach cycle to fill in some objects
The method header looks like this
private async Task createInvite(JsonValue item, string meetingid, List<MeetingInvitee> listInvitees)
i was trying to run this in parallel tasks like so
List<Task> ts = new List<Task>();
foreach (JsonValue item in invitees)
{
ts.Add(createInvite(item, meetingid, listInvitees));
}
await Task.WhenAll(ts);
but it seems it doesnt create tasks that run at all at same time. this takes about 10 seconds.
if instead i use this
//List<Task> ts = new List<Task>();
foreach (JsonValue item in invitees)
{
//ts.Add(createInvite(item, meetingid, listInvitees));
await createInvite(item, meetingid, listInvitees);
}
//await Task.WhenAll(ts);
it also takes about 10 seconds.
Am i not running several tasks at same time with my first option?
Edit
private async Task createInvite(JsonValue item, string meetingid, List<MeetingInvitee> listInvitees)
{
try
{
Stopwatch st = Stopwatch.StartNew();
MeetingInvitee org = new MeetingInvitee();
MeetingInvitee altorg = new MeetingInvitee();
JsonObject invitee2;
JsonObject invitee;
InviteeDB InviteeForDB = new InviteeDB();
GappService gappservice = new GappService();
MeetingInvitee inv = new MeetingInvitee();
JsonObject.TryParse(item.Stringify(), out invitee2);
invitee = invitee2["user"].GetObject();
if (invitee2.ContainsKey("_id"))
{
inv.id = invitee2["_id"].GetString();
InviteeForDB.Id = invitee2["_id"].GetString();
}
else
{
InviteeForDB.Id = invitee2["user"].GetString();
}
if (invitee2.ContainsKey("status"))
{
if (invitee2["status"].ValueType == JsonValueType.Null)
{
inv.status = string.Empty;
InviteeForDB.Status = string.Empty;
}
else
{
inv.status = invitee2["status"].GetString();
InviteeForDB.Status = invitee2["status"].GetString();
}
}
Stopwatch st2 = Stopwatch.StartNew();
if (invitee2.ContainsKey("user"))
{
if (invitee2["user"].ValueType != JsonValueType.Null)
{
User iUser = new User();
//iUser.Id = InviteeForDB.UserID;
JsonSerializerSettings sett = new JsonSerializerSettings();
sett.NullValueHandling = NullValueHandling.Ignore;
iUser = JsonConvert.DeserializeObject<User>(invitee.Stringify(), sett);
inv.user = iUser;
}
else
return;
}
else
return;
InviteeForDB.MeetingID = meetingid;
ds.inviteeRepository.Add(InviteeForDB);
listInvitees.Add(inv);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}