Skip to main content
added 359 characters in body
Source Link
Carlos
  • 466
  • 2
  • 9
  • 21

Found the issue: As soon as I iterate through the models list via AsParallel().ForAll , it might happen (depends on the CPU workload) that the task to create (async) has not been yet created but is being accessed by the WhenAll method (async too), and retrievestherefore throws the exception mentioned above. This happens sometimes, as I said based on, according to the CPU workload. To: In order to easily reproduce the issue easily, II've created a unit test thatwhich calls to the problematic method up to 50 times, and usually fails beforewithin the first 10 runs. In order to To solve the issue, I I've replaced the useless (because in this scenario does not bring anything relevant) function AsParallel().ForAll by ForEachand and the issue does not happen anymore because the taskstasks are always createdcreated following a sequence (not executed though, they are executed asynchronously), no matter the CPU workload. Code will be as follows, to solve the issue:

List<Model> ftpModels = _service.CreateFtpModel(rowsFromDB); List<Task> asyncTasks = new List<Task>(); ftpModels.ForEach(p => { asyncTasks.Add(DoStepsAsync(p)); }); // Wait for all the tasks to finish. await Task.WhenAll(asyncTasks.Where(p => p != null)); 

I hope it helps in the future to other people.

Found the issue: As soon as I iterate through the models list via AsParallel().ForAll , it might happen (depends on the CPU workload) the task to create has not been yet created but is being accessed by the WhenAll method, and retrieves the exception mentioned above. This happens sometimes, as I said based on the CPU workload. To reproduce the issue easily, I created a unit test that calls to the problematic method up to 50 times, and usually fails before 10 runs. In order to solve the issue, I replaced the AsParallel().ForAll by ForEachand the issue does not happen anymore because the tasks are always created, no matter the CPU workload. Code will be as follows, to solve the issue:

List<Model> ftpModels = _service.CreateFtpModel(rowsFromDB); List<Task> asyncTasks = new List<Task>(); ftpModels.ForEach(p => { asyncTasks.Add(DoStepsAsync(p)); }); // Wait for all the tasks to finish. await Task.WhenAll(asyncTasks.Where(p => p != null)); 

I hope it helps in the future to other people.

Found the issue: As soon as I iterate through the models list via AsParallel().ForAll , it might happen (depends on the CPU workload) that the task to create (async) has not been yet created but is being accessed by the WhenAll method (async too), therefore throws the exception mentioned above. This happens sometimes, as I said, according to the CPU workload: In order to easily reproduce the issue , I've created a unit test which calls the problematic method up to 50 times, and usually fails within the first 10 runs. To solve the issue, I I've replaced the useless (because in this scenario does not bring anything relevant) function AsParallel().ForAll by ForEach and the issue does not happen anymore because the tasks are always created following a sequence (not executed though, they are executed asynchronously), no matter the CPU workload. Code will be as follows, to solve the issue:

List<Model> ftpModels = _service.CreateFtpModel(rowsFromDB); List<Task> asyncTasks = new List<Task>(); ftpModels.ForEach(p => { asyncTasks.Add(DoStepsAsync(p)); }); // Wait for all the tasks to finish. await Task.WhenAll(asyncTasks.Where(p => p != null)); 

I hope it helps in the future to other people.

added 359 characters in body
Source Link
Carlos
  • 466
  • 2
  • 9
  • 21

Found the issue: As soon as I iterate through the models list via AsParallel().ForAll , it might happen (depends on the CPU workload) the task to create has not been yet created but is being accessed by the WhenAll method, and retrieves the exception mentioned above. This happens sometimes, as I said based on the CPU workload. To reproduce the issue easily, I created a unit test that calls to the problematic method up to 50 times, and usually fails before 10 runs. In order to solve the issue, I replaced the AsParallel().ForAll by ForEachand the issue does not happen anymore because the tasks are always created, no matter the CPU workload. Code will be as follows, to solve the issue:

List<Model> ftpModels = _service.CreateFtpModel(rowsFromDB); List<Task> asyncTasks = new List<Task>(); ftpModels.ForEach(p => { asyncTasks.Add(DoStepsAsync(p)); }); // Wait for all the tasks to finish. await Task.WhenAll(asyncTasks.Where(p => p != null)); 

I hope it helps in the future to other guyspeople.

Found the issue: As soon as I iterate through the models list via AsParallel().ForAll , it might happen (depends on the CPU workload) the task to create has not been yet created but is being accessed by the WhenAll method, and retrieves the exception mentioned above. This happens sometimes, as I said based on the CPU workload. To reproduce the issue easily, I created a unit test that calls to the problematic method up to 50 times, and usually fails before 10 runs. In order to solve the issue, I replaced the AsParallel().ForAll by ForEachand the issue does not happen anymore because the tasks are always created, no matter the CPU workload. I hope it helps in the future to other guys

Found the issue: As soon as I iterate through the models list via AsParallel().ForAll , it might happen (depends on the CPU workload) the task to create has not been yet created but is being accessed by the WhenAll method, and retrieves the exception mentioned above. This happens sometimes, as I said based on the CPU workload. To reproduce the issue easily, I created a unit test that calls to the problematic method up to 50 times, and usually fails before 10 runs. In order to solve the issue, I replaced the AsParallel().ForAll by ForEachand the issue does not happen anymore because the tasks are always created, no matter the CPU workload. Code will be as follows, to solve the issue:

List<Model> ftpModels = _service.CreateFtpModel(rowsFromDB); List<Task> asyncTasks = new List<Task>(); ftpModels.ForEach(p => { asyncTasks.Add(DoStepsAsync(p)); }); // Wait for all the tasks to finish. await Task.WhenAll(asyncTasks.Where(p => p != null)); 

I hope it helps in the future to other people.

Source Link
Carlos
  • 466
  • 2
  • 9
  • 21

Found the issue: As soon as I iterate through the models list via AsParallel().ForAll , it might happen (depends on the CPU workload) the task to create has not been yet created but is being accessed by the WhenAll method, and retrieves the exception mentioned above. This happens sometimes, as I said based on the CPU workload. To reproduce the issue easily, I created a unit test that calls to the problematic method up to 50 times, and usually fails before 10 runs. In order to solve the issue, I replaced the AsParallel().ForAll by ForEachand the issue does not happen anymore because the tasks are always created, no matter the CPU workload. I hope it helps in the future to other guys