I want to do a method that will replace this-
for (var i = 0; i < seconds; i++)
{
...... // code that should run every second
Thread.Sleep(1000);
}
So I wrote the following method:
public static void DoEverySecond(int seconds, Action action)
{
for (var i = 0; i < seconds; i++)
{
action.Invoke();
Thread.Sleep(1000);
}
}
and now every time that I want to do something every second I can just call -
HelperClass.DoEverySecond(5, () =>
{
Console.Write("Hellow")
});
the problem is that when the action contains return, the loop doesn't stop. It's just getting out from the action and continues to the next iteration.
HelperClass.DoEverySecond(5, () =>
{
return;
});