簡體   English   中英

運行並行任務C#

[英]Running parallel tasks c#

在Windows store應用程序項目中,我有一個函數,我在一個foreach周期內多次調用以填充一些對象

方法頭看起來像這樣

private async Task createInvite(JsonValue item, string meetingid, List<MeetingInvitee> listInvitees)

我試圖在像這樣的並行任務中運行

List<Task> ts = new List<Task>();
foreach (JsonValue item in invitees)
{
    ts.Add(createInvite(item, meetingid, listInvitees));
}

await Task.WhenAll(ts);

但是它似乎並沒有創建同時運行的任務。 這大約需要10秒鍾。

相反,如果我用這個

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

這也需要大約10秒鍾。

我的第一個選擇不是同時運行多個任務嗎?

編輯

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

您的代碼實際上不是異步的(沒有await語句),因此所有工作都在for循環內的初始createInvite()調用中for

您可以使用Parallel.ForEach()使它實際上並行運行,但這會破壞您的代碼,因為它實際上不是線程安全的。

您的方法並沒有真正的異步。 簡單地添加async修飾符並不能神奇地使此異步或並行執行,這是我認為您要嘗試的操作。 它所做的只是向編譯器發出信號,即需要生成狀態機。 該代碼將完全同步執行。

您可以使用Task.RunParallel.ForEach在線程池線程上顯式調用您的方法,但是查看您的代碼看起來根本就不是線程安全的。 在進行任何進一步的操作之前,請嘗試將CreateInvite隔離為實際上是線程安全的,然后研究上面提到的選項之一。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM