简体   繁体   English

运行并行任务C#

[英]Running parallel tasks c#

In a windows store app project i have a function that i call several times inside a foreach cycle to fill in some objects 在Windows store应用程序项目中,我有一个函数,我在一个foreach周期内多次调用以填充一些对象

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. 这大约需要10秒钟。

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. 这也需要大约10秒钟。

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);
    }
}

Your code isn't actually async (it has no await statements), so all of the work is happening in initial createInvite() call inside the for loop. 您的代码实际上不是异步的(没有await语句),因此所有工作都在for循环内的初始createInvite()调用中for

You can make that actually run in parallel using Parallel.ForEach() , but that will break your code, because it isn't actually thread-safe. 您可以使用Parallel.ForEach()使它实际上并行运行,但这会破坏您的代码,因为它实际上不是线程安全的。

There is nothing truly async about your method. 您的方法并没有真正的异步。 Simply adding the async modifier doesn't magically make this async, or execute in parallel which is what I think you're trying to do. 简单地添加async修饰符并不能神奇地使此异步或并行执行,这是我认为您要尝试的操作。 All it does is signal the compiler that a state-machine needs to be generated. 它所做的只是向编译器发出信号,即需要生成状态机。 This code, as is, will execute completely synchronously. 该代码将完全同步执行。

You can explicitly invoke your method on a thread-pool thread using Task.Run or Parallel.ForEach , but looking at your code it doesn't look as if it would be thread-safe at all to do that. 您可以使用Task.RunParallel.ForEach在线程池线程上显式调用您的方法,但是查看您的代码看起来根本就不是线程安全的。 Before going any further, try to isolate CreateInvite to actually be thread-safe, then look into one of the options mentioned above. 在进行任何进一步的操作之前,请尝试将CreateInvite隔离为实际上是线程安全的,然后研究上面提到的选项之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM