Skip to content

Commit

Permalink
Fix Service.cs to not modify collection while enumerating it (Power…
Browse files Browse the repository at this point in the history
  • Loading branch information
nxtn committed Mar 26, 2020
1 parent 668d72c commit 175efca
Showing 1 changed file with 6 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1087,17 +1087,9 @@ internal List<ServiceController> DoStopService(ServiceController serviceControll
/// 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>
Expand All @@ -1106,14 +1098,10 @@ private bool HaveAllDependentServicesStopped(ICollection<ServiceController> depe
/// <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>
Expand Down

0 comments on commit 175efca

Please sign in to comment.