Files
sbox-public/engine/Sandbox.System/Extend/EnumerableExtensions.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

58 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using System.Reflection;
namespace Sandbox;
public static partial class SandboxSystemExtensions
{
/// <summary>
/// Runs each task on this thread but only execute a set amount at a time
/// </summary>
public static async Task ForEachTaskAsync<T>( this IEnumerable<T> source, Func<T, Task> body, int maxRunning = 8, CancellationToken token = default )
{
var tasks = new List<Task>();
foreach ( var item in source )
{
var t = body( item );
tasks.Add( t );
while ( tasks.Count >= maxRunning )
{
await Task.WhenAny( tasks );
tasks.RemoveAll( x => x.IsCompleted );
}
token.ThrowIfCancellationRequested();
}
await Task.WhenAll( tasks );
token.ThrowIfCancellationRequested();
}
/// <summary>Finds the first common base type of the given types.</summary>
/// <param name="types">The types.</param>
/// <returns>The common base type.</returns>
public static Type GetCommonBaseType( this IEnumerable<Type> types )
{
types = types.ToList();
var baseType = types.First();
while ( baseType != typeof( object ) && baseType != null )
{
if ( types.All( t => baseType.GetTypeInfo().IsAssignableFrom( t.GetTypeInfo() ) ) )
{
return baseType;
}
baseType = baseType.GetTypeInfo().BaseType;
}
return typeof( object );
}
}