rj-action-library/Runtime/Tools/Safe.cs

41 lines
825 B
C#
Raw Normal View History

2024-08-11 17:38:06 +00:00
using System.Collections;
using System.Collections.Generic;
using System;
using System.Reflection;
using System.Text.RegularExpressions;
namespace Rokojori
{
public class Safe
{
static readonly int maxWhileIterations = 1000 * 1000;
2025-01-03 12:09:23 +00:00
public static void While( Func<bool> condition, System.Action action, System.Action onTooManyIterations = null )
2024-08-11 17:38:06 +00:00
{
2025-01-13 18:11:12 +00:00
if ( condition == null || action == null )
{
return;
}
2024-08-11 17:38:06 +00:00
var it = 0;
while ( it < Safe.maxWhileIterations && condition() )
{
2025-01-13 18:11:12 +00:00
action();
2024-08-11 17:38:06 +00:00
it++;
}
if ( it >= Safe.maxWhileIterations )
{
RJLog.LogWithFullTrace( "Loop with too many iterations" );
if ( onTooManyIterations != null )
{
onTooManyIterations();
}
}
}
}
}