Fix Service.cs to not modify collection while enumerating it (#11851)

This commit is contained in:
Next Turn
2020-03-26 11:17:58 -07:00
committed by GitHub
parent 668d72c4af
commit 175efca71f
@@ -1087,17 +1087,9 @@ namespace Microsoft.PowerShell.Commands
/// True if all dependent services are stopped
/// False if not all dependent services are stopped
/// </returns>
private bool HaveAllDependentServicesStopped(ICollection<ServiceController> dependentServices)
private bool HaveAllDependentServicesStopped(ServiceController[] dependentServices)
{
foreach (ServiceController service in dependentServices)
{
if (service.Status != ServiceControllerStatus.Stopped)
{
return false;
}
}
return true;
return Array.TrueForAll(dependentServices, service => service.Status == ServiceControllerStatus.Stopped);
}
/// <summary>
@@ -1106,14 +1098,10 @@ namespace Microsoft.PowerShell.Commands
/// <param name="services">A list of services.</param>
internal void RemoveNotStoppedServices(List<ServiceController> services)
{
foreach (ServiceController service in services)
{
if (service.Status != ServiceControllerStatus.Stopped &&
service.Status != ServiceControllerStatus.StopPending)
{
services.Remove(service);
}
}
// You shall not modify a collection during enumeration.
services.RemoveAll(service =>
service.Status != ServiceControllerStatus.Stopped &&
service.Status != ServiceControllerStatus.StopPending);
}
/// <summary>