/*######################################
# Duck SecurityTimer - Version 1 #
# Not for release and renaming #
# Breakz0ne did coded this, none else #
########################################*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
namespace DuckEmulator.Security
{
class SecurityTimer
{
#region Struct
private int Interval = new ushort();
private Timer priv_timer = new Timer();
private Action OnInterval;
private Boolean NoCycler = true;
private Object Sync = new Object();
private AsyncCallback Callback;
internal Object GetObject()
{
return priv_timer.GetLifetimeService();
}
#endregion
///
/// Timer thats raises every short time
///
///
///
internal SecurityTimer(int IntervalTimer, Action OnIntervalTimer)
{
Interval = IntervalTimer;
OnInterval = OnIntervalTimer;
Setup();
}
///
/// Timer thats raises every short time with- or with out cyclin'
///
///
///
internal SecurityTimer(int IntervalTimer, Action OnIntervalTimer, bool NoCycle)
{
Interval = IntervalTimer;
OnInterval = OnIntervalTimer;
NoCycler = NoCycle;
}
#region Private Summary
private void Setup()
{
Callback = new AsyncCallback(OnAsync);
priv_timer.Interval = Interval;
priv_timer.Elapsed += new ElapsedEventHandler(OntimerTick);
priv_timer.AutoReset = NoCycler;
}
private void OntimerTick(object sender, ElapsedEventArgs e)
{
try { Sync = OnInterval.BeginInvoke(Callback, Sync).AsyncState; }
catch { Dispose(); }
}
private void OnAsync(IAsyncResult Result)
{
try { OnInterval.EndInvoke(Result); }
catch { Dispose(); }
}
#endregion
///
/// Stop the Timer for cyclin'
///
///
internal void Freeze(bool Dispose)
{
priv_timer.Enabled = false;
if (Dispose)
this.Dispose();
}
///
/// Let the time wait.
///
///
internal void Sleep(int Milisec)
{
Freeze(false);
for (int i = 0; i < Milisec; i++) { }
priv_timer.Enabled = true;
}
///
/// Starts the Timer
///
internal void Release()
{
priv_timer.Start();
}
///
/// Releases all Objects in this Timer.
///
internal void Dispose()
{
if (priv_timer != null)
{
priv_timer = null;
}
if (Callback != null)
{
Callback = null;
}
}
}
}