mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Fix Service.cs to not modify collection while enumerating it (#11851)
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user