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
{
///
/// Runs each task on this thread but only execute a set amount at a time
///
public static async Task ForEachTaskAsync( this IEnumerable source, Func body, int maxRunning = 8, CancellationToken token = default )
{
var tasks = new List();
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();
}
/// Finds the first common base type of the given types.
/// The types.
/// The common base type.
public static Type GetCommonBaseType( this IEnumerable 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 );
}
}