我有一个场景,我必须在学生列表中并行/独立地运行学生列表。但是,当我用下面的代码运行这些程序时,程序没有正确完成就结束了。C#等待进程毫无例外地退出
public async Task ProcessStudents()
{
var students = await GetStudentsAsync().ConfigureAwait(false);
ProcessSingleStudent(students);
}
private static ProcessSingleStudent(IEnumerable<StudentModel> students)
{
students.ForEach(async student =>
{
await ValidateSingleStudentAsync(student).ConfigureAwait(false);
}
}
private async Task ValidateSingleStudentAsync(StudentModel student)
{
//Do some validations here
if(validate)
{
var updated = await UpdateStudentAsync(student).configureAwait(false); //<== This cause issue
}
}
当我看到UpdateStudentAsync
造成的问题,如果有F10
去这个方法不返回任何东西,控制台应用程序停止。即使我把每个电话都拨打try-catch
我什么都找不到。如果我进入每个调试点,我会得到预期的结果。
无法理解问题出在哪里。
哪里是'UpdateStudentAsync'的代码? –
请分享调用'ValidateSingleStudentAsync'的代码,并给我们提供关于主应用程序的更多信息,这意味着:如果'ValidateSingleStudentAsync'运行在临时上下文中,则运行在Windows Form App或Console App上 –
This是你需要的https://stackoverflow.com/a/39174582/782754 –