topmostfriend/TopMostFriend/ActionTimeout.cs
2022-08-26 02:01:04 +02:00

36 lines
973 B
C#

using System;
using System.Threading;
namespace TopMostFriend {
public class ActionTimeout {
private readonly Action Action;
private bool Continue = true;
private int Remaining = 0;
private const int STEP = 500;
public ActionTimeout(Action action, int timeout) {
Action = action ?? throw new ArgumentNullException(nameof(action));
if(timeout < 1)
throw new ArgumentException(@"Timeout must be a positive integer.", nameof(timeout));
Remaining = timeout;
new Thread(ThreadBody) { IsBackground = true }.Start();
}
private void ThreadBody() {
do {
Thread.Sleep(STEP);
Remaining -= STEP;
if(!Continue)
return;
} while(Remaining > 0);
Action.Invoke();
}
public void Cancel() {
Continue = false;
}
}
}