如何使一系列任务按顺序运行?

时间:2016-10-25 16:45:39

标签: c# asynchronous task-parallel-library

我想要按照a-z的顺序运行一系列Task

我想同步执行这些调用starting from 1 and ending at N

  • task1 DeleteSubProjects ... 1
    • task1 DeleteActivities ... 2
      • task1 DeleteActivityPersons ... 3
      • task2 DeleteThreads ... 4
        • task1 DeleteCommunicationThreadContentTags ... 5
        • task2 DeleteCommunicationThreadParticipants ... 6
        • task3 DeleteMessages
        • task4 删除(communicationThread)
      • task3 DeleteActivityDocuments
      • task4 DeleteActivityNotes
      • task5 DeleteActivityMachines
      • task6 DeleteActivityLinks
      • task7 DeleteActivityLinks
      • task8 删除(活动)
    • task2 DeleteSubProjectDocuments
    • task3 DeleteSubProjectNotes
    • task4 DeleteSubProjectPersons
    • task5 DeleteSubProjects
    • task6 删除(子项目)
  • task2 deleteProjectNotes
  • task3 deleteProjectDocuments
  • task4 deleteActivities
  • task5 deleteProjectPersons

这是代码的样子

public async Task DeleteProject(Project project)
{

    var deleteSubProjects = UnitOfWork.Instance.SubProjectService.DeleteSubProjects(project.SubProjects);
    var deleteProjectNotes = UnitOfWork.Instance.ProjectNoteService.DeleteProjectNotes(project.ProjectNotes);
    var deleteProjectDocuments = UnitOfWork.Instance.ProjectDocumentService.DeleteProjectDocuments(project.ProjectDocuments);
    var deleteActivities = UnitOfWork.Instance.ActivityService.DeleteActivities(project.Activities);
    var deleteProjectPersons = UnitOfWork.Instance.ProjectPersonService.DeleteProjectPersons(project.ProjectPersons);

    await Task.WhenAll(deleteSubProjects)
        .ContinueWith(_ => deleteProjectNotes)
        .ContinueWith(_ => deleteProjectDocuments)
        .ContinueWith(_ => deleteActivities)
        .ContinueWith(_ => deleteProjectPersons)
        .ContinueWith(_ => Delete(project)).Unwrap();            
}

子项目

public async Task DeleteSubProjects(IList<SubProject> subProjects)
{
    foreach (var subProject in subProjects.ToList())
    {
        await DeleteSubProject(subProject);
    }
}
public async Task DeleteSubProject(SubProject subProject)
{
    var task1 = UnitOfWork.Instance.ActivityService.DeleteActivities(subProject.Activities);
    var task2 = UnitOfWork.Instance.SubProjectDocumentService.DeleteSubProjectDocuments(subProject.SubProjectDocuments);
    var task3 = UnitOfWork.Instance.SubProjectNoteService.DeleteSubProjectNotes(subProject.SubProjectNotes);
    var task4 = UnitOfWork.Instance.SubProjectPersonService.DeleteSubProjectPersons(subProject.SubProjectPersons);
    var task5 = DeleteSubProjects(subProject.ChildSubProjects);
    var task6 = Delete(subProject);

    await Task.WhenAll(task1)
        .ContinueWith(_ => task2)
        .ContinueWith(_ => task3)
        .ContinueWith(_ => task4)
        .ContinueWith(_ => task5)
        .ContinueWith(_ => task6).Unwrap();
    //Delete(subProject);    
}

活动

public async Task DeleteActivities(IList<Activity> activities)
{
    foreach (var activity in activities)
    {
         await DeleteActivity(activity);
    }
}
public async Task DeleteActivity(Activity activity)
{
    var task1 = UnitOfWork.Instance.ActivityPersonService.DeleteActivityPersons(activity.ActivityPersons);
    var task2 = UnitOfWork.Instance.CommunicationThreadService.DeleteThreads(activity.CommunicationThreads);
    var task3 = UnitOfWork.Instance.ActivityDocumentService.DeleteActivityDocuments(activity.ActivityDocuments);
    var task4 = UnitOfWork.Instance.ActivityNoteService.DeleteActivityNotes(activity.ActivityNotes);
    var task5 = UnitOfWork.Instance.ActivityMachineService.DeleteActivityMachines(activity.ActivityMachines);
    var task6 = UnitOfWork.Instance.ActivityLinkService.DeleteActivityLinks(activity.SuccActivityLinks);
    var task7 = UnitOfWork.Instance.ActivityLinkService.DeleteActivityLinks(activity.PredActivityLinks);
    var task8 = Delete(activity);

    await Task.WhenAll(task1)
        .ContinueWith(_ => task2)
        .ContinueWith(_ => task3)
        .ContinueWith(_ => task4)
        .ContinueWith(_ => task5)
        .ContinueWith(_ => task6)
        .ContinueWith(_ => task7)
        .ContinueWith(_ => task8).Unwrap();
   // Delete(activity);
}

线程

internal async Task DeleteThreads(IList<CommunicationThread> threads)
{
    foreach (var thread in threads)
    {
      await  DeleteThread(thread);
    }
}
internal async Task DeleteThread(CommunicationThread communicationThread)
{
   var task1 = UnitOfWork.Instance.CommunicationThreadContentTagService.DeleteCommunicationThreadContentTags(communicationThread.CommunicationThreadContentTags);
   var task2 = UnitOfWork.Instance.CommunicationThreadParticipantService.DeleteCommunicationThreadParticipants(communicationThread.CommunicationThreadParticipants);
   var task3 = UnitOfWork.Instance.CommunicationMessageService.DeleteMessages(communicationThread.CommunicationMessages.Where(msg => msg.CommentOnMessageID == null).ToList());
   var task4 = Delete(communicationThread);

   await Task.WhenAll(task1)
        .ContinueWith(_ => task2)
        .ContinueWith(_ => task3)
        .ContinueWith(_ => task4).Unwrap();
}

1 个答案:

答案 0 :(得分:5)

而不是调用int size = 2; Eigen::MatrixXd covar(size,size); covar << 1, .5, .5, 1; normal_random_variable sample { covar }; std::cout << sample() << std::endl; std::cout << sample() << std::endl; 并为每个Task.WhenAll声明一个变量,而只需按照您需要的顺序Task - 例如:

await

现在,序列化此特定示例中的删除操作的顺序。