mirror of
https://github.com/flashwave/topmostfriend.git
synced 2024-11-21 17:43:08 +00:00
35 lines
973 B
C#
35 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;
|
|
}
|
|
}
|
|
}
|