mirror of
https://github.com/flashwave/topmostfriend.git
synced 2024-11-22 09:48:44 +00:00
36 lines
973 B
C#
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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|