Only last task runs!

后端 未结 1 502
孤街浪徒
孤街浪徒 2021-01-18 20:11

I am in desperate to find a solution to my problem.

Following is the code which generates different task for each item in List

1条回答
  •  梦毁少年i
    2021-01-18 21:05

    This might be a lambda scope problem.

    Have you tried to assign the viewModel to a local variable before passing it to the StartNew method

    ...
    Task validateMarked = Task.Factory.StartNew(() =>
    {
        foreach (AccountContactViewModel viewModel in selectedDataList)
        {
            var localViewModel = viewModel;
            if (localViewModel != null)
            {
                Task validate = Task.Factory.StartNew(
                    () => ValidateAccount(localViewModel),
                    (TaskCreationOptions)TaskContinuationOptions.AttachedToParent);
            }
        }
    });
    ...
    

    0 讨论(0)
提交回复
热议问题