mirror of
https://github.com/copyrighttxt/watrbx-game-engine.git
synced 2026-09-05 05:07:48 +00:00
59 lines
948 B
C#
59 lines
948 B
C#
using System;
|
|
using System.Threading;
|
|
|
|
namespace Roblox;
|
|
|
|
public class SelfDisposingTimer
|
|
{
|
|
private readonly Action action;
|
|
|
|
private Timer timer;
|
|
|
|
private TimeSpan period;
|
|
|
|
public SelfDisposingTimer(Action action, TimeSpan startTime, TimeSpan period)
|
|
{
|
|
this.action = action;
|
|
this.period = period;
|
|
timer = new Timer(delegate(object weakThis)
|
|
{
|
|
OnTimer((WeakReference)weakThis);
|
|
}, new WeakReference(this), startTime, period);
|
|
}
|
|
|
|
private static void OnTimer(WeakReference self)
|
|
{
|
|
if (self.Target is SelfDisposingTimer currentTimer)
|
|
{
|
|
currentTimer.action();
|
|
}
|
|
}
|
|
|
|
public bool Change(TimeSpan dueTime, TimeSpan period)
|
|
{
|
|
this.period = period;
|
|
return timer.Change(dueTime, period);
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
timer.Dispose();
|
|
timer = null;
|
|
}
|
|
|
|
~SelfDisposingTimer()
|
|
{
|
|
timer?.Dispose();
|
|
}
|
|
|
|
internal void Pause()
|
|
{
|
|
timer.Change(-1, -1);
|
|
}
|
|
|
|
internal void Unpause()
|
|
{
|
|
timer.Change(period, period);
|
|
}
|
|
}
|