我想要按照a-z的顺序运行一系列Task
。
我想同步执行这些调用starting from 1 and ending at N
。
这是代码的样子
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();
}
答案 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
现在,序列化此特定示例中的删除操作的顺序。