Code cleanup (#188)
This commit is contained in:
parent
df53c16620
commit
010fd9402a
185 changed files with 1052 additions and 1271 deletions
486
Projects/Server/Timer/Timer.cs
Normal file
486
Projects/Server/Timer/Timer.cs
Normal file
|
|
@ -0,0 +1,486 @@
|
|||
/***************************************************************************
|
||||
* Timer.cs
|
||||
* -------------------
|
||||
* begin : May 1, 2002
|
||||
* copyright : (C) The RunUO Software Team
|
||||
* email : info@runuo.com
|
||||
*
|
||||
* $Id$
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Server.Diagnostics;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public enum TimerPriority
|
||||
{
|
||||
EveryTick,
|
||||
TenMS,
|
||||
TwentyFiveMS,
|
||||
FiftyMS,
|
||||
TwoFiftyMS,
|
||||
OneSecond,
|
||||
FiveSeconds,
|
||||
OneMinute
|
||||
}
|
||||
|
||||
public partial class Timer
|
||||
{
|
||||
private static readonly Queue<Timer> m_Queue = new Queue<Timer>();
|
||||
|
||||
private static int m_QueueCountAtSlice;
|
||||
private long m_Delay;
|
||||
private int m_Index;
|
||||
private readonly int m_Count;
|
||||
private long m_Interval;
|
||||
private List<Timer> m_List;
|
||||
private long m_Next;
|
||||
private TimerPriority m_Priority;
|
||||
private bool m_PrioritySet;
|
||||
|
||||
private bool m_Queued;
|
||||
private bool m_Running;
|
||||
|
||||
public Timer(TimeSpan delay) : this(delay, TimeSpan.Zero, 1)
|
||||
{
|
||||
}
|
||||
|
||||
public Timer(TimeSpan delay, TimeSpan interval, int count = 0)
|
||||
{
|
||||
m_Delay = (long)delay.TotalMilliseconds;
|
||||
m_Interval = (long)interval.TotalMilliseconds;
|
||||
m_Count = count;
|
||||
|
||||
if (!m_PrioritySet)
|
||||
{
|
||||
m_Priority = ComputePriority(count == 1 ? delay : interval);
|
||||
m_PrioritySet = true;
|
||||
}
|
||||
|
||||
if (DefRegCreation)
|
||||
RegCreation();
|
||||
}
|
||||
|
||||
public TimerPriority Priority
|
||||
{
|
||||
get => m_Priority;
|
||||
set
|
||||
{
|
||||
if (!m_PrioritySet)
|
||||
m_PrioritySet = true;
|
||||
|
||||
if (m_Priority != value)
|
||||
{
|
||||
m_Priority = value;
|
||||
|
||||
if (m_Running)
|
||||
TimerThread.PriorityChange(this, (int)m_Priority);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime Next => DateTime.UtcNow + TimeSpan.FromMilliseconds(m_Next - Core.TickCount);
|
||||
|
||||
public TimeSpan Delay
|
||||
{
|
||||
get => TimeSpan.FromMilliseconds(m_Delay);
|
||||
set => m_Delay = (long)value.TotalMilliseconds;
|
||||
}
|
||||
|
||||
public TimeSpan Interval
|
||||
{
|
||||
get => TimeSpan.FromMilliseconds(m_Interval);
|
||||
set => m_Interval = (long)value.TotalMilliseconds;
|
||||
}
|
||||
|
||||
public bool Running
|
||||
{
|
||||
get => m_Running;
|
||||
set
|
||||
{
|
||||
if (value)
|
||||
Start();
|
||||
else
|
||||
Stop();
|
||||
}
|
||||
}
|
||||
|
||||
public static int BreakCount { get; set; } = 20000;
|
||||
|
||||
public virtual bool DefRegCreation => true;
|
||||
|
||||
private static string FormatDelegate(Delegate callback) =>
|
||||
callback == null ? "null" : $"{callback.Method.DeclaringType?.FullName ?? ""}.{callback.Method.Name}";
|
||||
|
||||
public static void DumpInfo(TextWriter tw)
|
||||
{
|
||||
TimerThread.DumpInfo(tw);
|
||||
}
|
||||
|
||||
public TimerProfile GetProfile()
|
||||
{
|
||||
if (!Core.Profiling) return null;
|
||||
|
||||
var name = ToString();
|
||||
|
||||
return TimerProfile.Acquire(name);
|
||||
}
|
||||
|
||||
public static void Slice()
|
||||
{
|
||||
lock (m_Queue)
|
||||
{
|
||||
m_QueueCountAtSlice = m_Queue.Count;
|
||||
|
||||
var index = 0;
|
||||
|
||||
while (index < BreakCount && m_Queue.Count != 0)
|
||||
{
|
||||
var t = m_Queue.Dequeue();
|
||||
var prof = t.GetProfile();
|
||||
|
||||
prof?.Start();
|
||||
|
||||
t.OnTick();
|
||||
t.m_Queued = false;
|
||||
++index;
|
||||
|
||||
prof?.Finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RegCreation()
|
||||
{
|
||||
var prof = GetProfile();
|
||||
|
||||
if (prof != null) prof.Created++;
|
||||
}
|
||||
|
||||
public override string ToString() => GetType().FullName ?? "";
|
||||
|
||||
public static TimerPriority ComputePriority(TimeSpan ts)
|
||||
{
|
||||
if (ts >= TimeSpan.FromMinutes(1.0))
|
||||
return TimerPriority.FiveSeconds;
|
||||
|
||||
if (ts >= TimeSpan.FromSeconds(10.0))
|
||||
return TimerPriority.OneSecond;
|
||||
|
||||
if (ts >= TimeSpan.FromSeconds(5.0))
|
||||
return TimerPriority.TwoFiftyMS;
|
||||
|
||||
if (ts >= TimeSpan.FromSeconds(2.5))
|
||||
return TimerPriority.FiftyMS;
|
||||
|
||||
if (ts >= TimeSpan.FromSeconds(1.0))
|
||||
return TimerPriority.TwentyFiveMS;
|
||||
|
||||
if (ts >= TimeSpan.FromSeconds(0.5))
|
||||
return TimerPriority.TenMS;
|
||||
|
||||
return TimerPriority.EveryTick;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
if (!m_Running)
|
||||
{
|
||||
m_Running = true;
|
||||
TimerThread.AddTimer(this);
|
||||
|
||||
var prof = GetProfile();
|
||||
|
||||
if (prof != null) prof.Started++;
|
||||
}
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
if (m_Running)
|
||||
{
|
||||
m_Running = false;
|
||||
TimerThread.RemoveTimer(this);
|
||||
|
||||
var prof = GetProfile();
|
||||
|
||||
if (prof != null) prof.Stopped++;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnTick()
|
||||
{
|
||||
}
|
||||
|
||||
public class TimerThread
|
||||
{
|
||||
private static readonly Dictionary<Timer, TimerChangeEntry> m_Changed = new Dictionary<Timer, TimerChangeEntry>();
|
||||
|
||||
private static readonly long[] m_NextPriorities = new long[8];
|
||||
|
||||
private static readonly long[] m_PriorityDelays =
|
||||
{
|
||||
0,
|
||||
10,
|
||||
25,
|
||||
50,
|
||||
250,
|
||||
1000,
|
||||
5000,
|
||||
60000
|
||||
};
|
||||
|
||||
private static readonly List<Timer>[] m_Timers =
|
||||
{
|
||||
new List<Timer>(),
|
||||
new List<Timer>(),
|
||||
new List<Timer>(),
|
||||
new List<Timer>(),
|
||||
new List<Timer>(),
|
||||
new List<Timer>(),
|
||||
new List<Timer>(),
|
||||
new List<Timer>()
|
||||
};
|
||||
|
||||
private static readonly AutoResetEvent m_Signal = new AutoResetEvent(false);
|
||||
|
||||
public static void DumpInfo(TextWriter tw)
|
||||
{
|
||||
for (var i = 0; i < 8; ++i)
|
||||
{
|
||||
tw.WriteLine("Priority: {0}", (TimerPriority)i);
|
||||
tw.WriteLine();
|
||||
|
||||
var hash = new Dictionary<string, List<Timer>>();
|
||||
|
||||
for (var j = 0; j < m_Timers[i].Count; ++j)
|
||||
{
|
||||
var t = m_Timers[i][j];
|
||||
|
||||
var key = t.ToString();
|
||||
|
||||
if (!hash.TryGetValue(key, out var list))
|
||||
hash[key] = list = new List<Timer>();
|
||||
|
||||
list.Add(t);
|
||||
}
|
||||
|
||||
foreach (var kv in hash)
|
||||
{
|
||||
var key = kv.Key;
|
||||
var list = kv.Value;
|
||||
|
||||
tw.WriteLine("Type: {0}; Count: {1}; Percent: {2}%", key, list.Count,
|
||||
(int)(100 * (list.Count / (double)m_Timers[i].Count)));
|
||||
}
|
||||
|
||||
tw.WriteLine();
|
||||
tw.WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Change(Timer t, int newIndex, bool isAdd)
|
||||
{
|
||||
lock (m_Changed)
|
||||
{
|
||||
m_Changed[t] = TimerChangeEntry.GetInstance(t, newIndex, isAdd);
|
||||
}
|
||||
|
||||
m_Signal.Set();
|
||||
}
|
||||
|
||||
public static void AddTimer(Timer t)
|
||||
{
|
||||
Change(t, (int)t.Priority, true);
|
||||
}
|
||||
|
||||
public static void PriorityChange(Timer t, int newPrio)
|
||||
{
|
||||
Change(t, newPrio, false);
|
||||
}
|
||||
|
||||
public static void RemoveTimer(Timer t)
|
||||
{
|
||||
Change(t, -1, false);
|
||||
}
|
||||
|
||||
private static void ProcessChanged()
|
||||
{
|
||||
lock (m_Changed)
|
||||
{
|
||||
var curTicks = Core.TickCount;
|
||||
|
||||
foreach (var tce in m_Changed.Values)
|
||||
{
|
||||
var timer = tce.m_Timer;
|
||||
var newIndex = tce.m_NewIndex;
|
||||
|
||||
timer.m_List?.Remove(timer);
|
||||
|
||||
if (tce.m_IsAdd)
|
||||
{
|
||||
timer.m_Next = curTicks + timer.m_Delay;
|
||||
timer.m_Index = 0;
|
||||
}
|
||||
|
||||
if (newIndex >= 0)
|
||||
{
|
||||
timer.m_List = m_Timers[newIndex];
|
||||
timer.m_List.Add(timer);
|
||||
}
|
||||
else
|
||||
{
|
||||
timer.m_List = null;
|
||||
}
|
||||
|
||||
tce.Free();
|
||||
}
|
||||
|
||||
m_Changed.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Set()
|
||||
{
|
||||
m_Signal.Set();
|
||||
}
|
||||
|
||||
public void TimerMain()
|
||||
{
|
||||
while (!Core.Closing)
|
||||
{
|
||||
if (World.Loading || World.Saving)
|
||||
{
|
||||
m_Signal.WaitOne(1, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
ProcessChanged();
|
||||
|
||||
var loaded = false;
|
||||
|
||||
for (var i = 0; i < m_Timers.Length; i++)
|
||||
{
|
||||
var now = Core.TickCount;
|
||||
if (now < m_NextPriorities[i])
|
||||
break;
|
||||
|
||||
m_NextPriorities[i] = now + m_PriorityDelays[i];
|
||||
|
||||
for (var j = 0; j < m_Timers[i].Count; j++)
|
||||
{
|
||||
var t = m_Timers[i][j];
|
||||
|
||||
if (!t.m_Queued && now > t.m_Next)
|
||||
{
|
||||
t.m_Queued = true;
|
||||
|
||||
lock (m_Queue)
|
||||
{
|
||||
m_Queue.Enqueue(t);
|
||||
}
|
||||
|
||||
loaded = true;
|
||||
|
||||
if (t.m_Count != 0 && ++t.m_Index >= t.m_Count)
|
||||
t.Stop();
|
||||
else
|
||||
t.m_Next = now + t.m_Interval;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (loaded)
|
||||
Core.Set();
|
||||
|
||||
m_Signal.WaitOne(1, false);
|
||||
}
|
||||
}
|
||||
|
||||
private class TimerChangeEntry
|
||||
{
|
||||
private static readonly Queue<TimerChangeEntry> m_InstancePool = new Queue<TimerChangeEntry>();
|
||||
public bool m_IsAdd;
|
||||
public int m_NewIndex;
|
||||
public Timer m_Timer;
|
||||
|
||||
private TimerChangeEntry(Timer t, int newIndex, bool isAdd)
|
||||
{
|
||||
m_Timer = t;
|
||||
m_NewIndex = newIndex;
|
||||
m_IsAdd = isAdd;
|
||||
}
|
||||
|
||||
public void Free()
|
||||
{
|
||||
lock (m_InstancePool)
|
||||
{
|
||||
if (m_InstancePool.Count < 200) // Arbitrary
|
||||
m_InstancePool.Enqueue(this);
|
||||
}
|
||||
}
|
||||
|
||||
public static TimerChangeEntry GetInstance(Timer t, int newIndex, bool isAdd)
|
||||
{
|
||||
TimerChangeEntry e = null;
|
||||
|
||||
lock (m_InstancePool)
|
||||
{
|
||||
if (m_InstancePool.Count > 0) e = m_InstancePool.Dequeue();
|
||||
}
|
||||
|
||||
if (e != null)
|
||||
{
|
||||
e.m_Timer = t;
|
||||
e.m_NewIndex = newIndex;
|
||||
e.m_IsAdd = isAdd;
|
||||
}
|
||||
else
|
||||
{
|
||||
e = new TimerChangeEntry(t, newIndex, isAdd);
|
||||
}
|
||||
|
||||
return e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class DelayTaskTimer : Timer
|
||||
{
|
||||
private readonly TaskCompletionSource<DelayTaskTimer> m_TaskCompleter;
|
||||
|
||||
public Task Task => m_TaskCompleter.Task;
|
||||
|
||||
public DelayTaskTimer(TimeSpan delay) : base(delay) => m_TaskCompleter = new TaskCompletionSource<DelayTaskTimer>();
|
||||
|
||||
protected override void OnTick()
|
||||
{
|
||||
m_TaskCompleter.SetResult(this);
|
||||
}
|
||||
}
|
||||
|
||||
public static Task Pause(int ms) => Pause(TimeSpan.FromMilliseconds(ms));
|
||||
|
||||
public static Task Pause(TimeSpan ms)
|
||||
{
|
||||
var t = new DelayTaskTimer(ms);
|
||||
t.Start();
|
||||
return t.Task;
|
||||
}
|
||||
}
|
||||
}
|
||||
268
Projects/Server/Timer/TimerDelayCalls.cs
Normal file
268
Projects/Server/Timer/TimerDelayCalls.cs
Normal file
|
|
@ -0,0 +1,268 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2020 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: TimerDelayCalls.cs - Created: 2020/07/31 - Updated: 2020/07/31 *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public delegate void TimerCallback();
|
||||
public delegate void TimerStateCallback<in T>(T state);
|
||||
public delegate void TimerStateCallback<in T1, in T2>(T1 t1, T2 t2);
|
||||
public delegate void TimerStateCallback<in T1, in T2, in T3>(T1 t1, T2 t2, T3 t3);
|
||||
public delegate void TimerStateCallback<in T1, in T2, in T3, in T4>(T1 t1, T2 t2, T3 t3, T4 t4);
|
||||
|
||||
public partial class Timer
|
||||
{
|
||||
public static Timer DelayCall(TimerCallback callback) => DelayCall(TimeSpan.Zero, TimeSpan.Zero, 1, callback);
|
||||
|
||||
public static Timer DelayCall(TimeSpan delay, TimerCallback callback) => DelayCall(delay, TimeSpan.Zero, 1, callback);
|
||||
|
||||
public static Timer DelayCall(TimeSpan delay, TimeSpan interval, TimerCallback callback) =>
|
||||
DelayCall(delay, interval, 0, callback);
|
||||
|
||||
public static Timer DelayCall(TimeSpan delay, TimeSpan interval, int count, TimerCallback callback)
|
||||
{
|
||||
Timer t = new DelayCallTimer(delay, interval, count, callback);
|
||||
|
||||
t.Priority = ComputePriority(count == 1 ? delay : interval);
|
||||
t.Start();
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
private class DelayCallTimer : Timer
|
||||
{
|
||||
public DelayCallTimer(TimeSpan delay, TimeSpan interval, int count, TimerCallback callback) : base(delay,
|
||||
interval, count)
|
||||
{
|
||||
Callback = callback;
|
||||
RegCreation();
|
||||
}
|
||||
|
||||
public TimerCallback Callback { get; }
|
||||
|
||||
public override bool DefRegCreation => false;
|
||||
|
||||
protected override void OnTick()
|
||||
{
|
||||
Callback?.Invoke();
|
||||
}
|
||||
|
||||
public override string ToString() => $"DelayCallTimer[{FormatDelegate(Callback)}]";
|
||||
}
|
||||
|
||||
public static Timer DelayCall<T>(TimerStateCallback<T> callback, T state) =>
|
||||
DelayCall(TimeSpan.Zero, TimeSpan.Zero, 1, callback, state);
|
||||
|
||||
public static Timer DelayCall<T>(TimeSpan delay, TimerStateCallback<T> callback, T state) =>
|
||||
DelayCall(delay, TimeSpan.Zero, 1, callback, state);
|
||||
|
||||
public static Timer DelayCall<T>(TimeSpan delay, TimeSpan interval, TimerStateCallback<T> callback, T state) =>
|
||||
DelayCall(delay, interval, 0, callback, state);
|
||||
|
||||
public static Timer DelayCall<T>(TimeSpan delay, TimeSpan interval, int count, TimerStateCallback<T> callback,
|
||||
T state)
|
||||
{
|
||||
Timer t = new DelayStateCallTimer<T>(delay, interval, count, callback, state);
|
||||
|
||||
t.Priority = ComputePriority(count == 1 ? delay : interval);
|
||||
|
||||
t.Start();
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
private class DelayStateCallTimer<T> : Timer
|
||||
{
|
||||
private readonly T m_State;
|
||||
|
||||
public DelayStateCallTimer(TimeSpan delay, TimeSpan interval, int count, TimerStateCallback<T> callback, T state)
|
||||
: base(delay, interval, count)
|
||||
{
|
||||
Callback = callback;
|
||||
m_State = state;
|
||||
|
||||
RegCreation();
|
||||
}
|
||||
|
||||
public TimerStateCallback<T> Callback { get; }
|
||||
|
||||
public override bool DefRegCreation => false;
|
||||
|
||||
protected override void OnTick()
|
||||
{
|
||||
Callback?.Invoke(m_State);
|
||||
}
|
||||
|
||||
public override string ToString() => $"DelayStateCall[{FormatDelegate(Callback)}]";
|
||||
}
|
||||
|
||||
public static Timer DelayCall<T1, T2>(TimerStateCallback<T1, T2> callback, T1 t1, T2 t2) =>
|
||||
DelayCall(TimeSpan.Zero, TimeSpan.Zero, 1, callback, t1, t2);
|
||||
|
||||
public static Timer DelayCall<T1, T2>(TimeSpan delay, TimerStateCallback<T1, T2> callback, T1 t1, T2 t2) =>
|
||||
DelayCall(delay, TimeSpan.Zero, 1, callback, t1, t2);
|
||||
|
||||
public static Timer DelayCall<T1, T2>(TimeSpan delay, TimeSpan interval, TimerStateCallback<T1, T2> callback,
|
||||
T1 t1, T2 t2) => DelayCall(delay, interval, 0, callback, t1, t2);
|
||||
|
||||
public static Timer DelayCall<T1, T2>(TimeSpan delay, TimeSpan interval, int count, TimerStateCallback<T1, T2> callback,
|
||||
T1 t1, T2 t2)
|
||||
{
|
||||
Timer t = new DelayStateCallTimer<T1, T2>(delay, interval, count, callback, t1, t2);
|
||||
|
||||
t.Priority = ComputePriority(count == 1 ? delay : interval);
|
||||
|
||||
t.Start();
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
private class DelayStateCallTimer<T1, T2> : Timer
|
||||
{
|
||||
private readonly T1 m_T1;
|
||||
private readonly T2 m_T2;
|
||||
|
||||
public DelayStateCallTimer(TimeSpan delay, TimeSpan interval, int count, TimerStateCallback<T1, T2> callback,
|
||||
T1 t1, T2 t2) : base(delay, interval, count)
|
||||
{
|
||||
Callback = callback;
|
||||
m_T1 = t1;
|
||||
m_T2 = t2;
|
||||
|
||||
RegCreation();
|
||||
}
|
||||
|
||||
public TimerStateCallback<T1, T2> Callback { get; }
|
||||
|
||||
public override bool DefRegCreation => false;
|
||||
|
||||
protected override void OnTick()
|
||||
{
|
||||
Callback?.Invoke(m_T1, m_T2);
|
||||
}
|
||||
|
||||
public override string ToString() => $"DelayStateCall[{FormatDelegate(Callback)}]";
|
||||
}
|
||||
|
||||
public static Timer DelayCall<T1, T2, T3>(TimerStateCallback<T1, T2, T3> callback, T1 t1, T2 t2, T3 t3) =>
|
||||
DelayCall(TimeSpan.Zero, TimeSpan.Zero, 1, callback, t1, t2, t3);
|
||||
|
||||
public static Timer DelayCall<T1, T2, T3>(TimeSpan delay, TimerStateCallback<T1, T2, T3> callback, T1 t1, T2 t2, T3 t3) =>
|
||||
DelayCall(delay, TimeSpan.Zero, 1, callback, t1, t2, t3);
|
||||
|
||||
public static Timer DelayCall<T1, T2, T3>(TimeSpan delay, TimeSpan interval, TimerStateCallback<T1, T2, T3> callback,
|
||||
T1 t1, T2 t2, T3 t3) => DelayCall(delay, interval, 0, callback, t1, t2, t3);
|
||||
|
||||
public static Timer DelayCall<T1, T2, T3>(TimeSpan delay, TimeSpan interval, int count,
|
||||
TimerStateCallback<T1, T2, T3> callback, T1 t1, T2 t2, T3 t3)
|
||||
{
|
||||
Timer t = new DelayStateCallTimer<T1, T2, T3>(delay, interval, count, callback, t1, t2, t3);
|
||||
|
||||
t.Priority = ComputePriority(count == 1 ? delay : interval);
|
||||
|
||||
t.Start();
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
private class DelayStateCallTimer<T1, T2, T3> : Timer
|
||||
{
|
||||
private readonly T1 m_T1;
|
||||
private readonly T2 m_T2;
|
||||
private readonly T3 m_T3;
|
||||
|
||||
public DelayStateCallTimer(TimeSpan delay, TimeSpan interval, int count, TimerStateCallback<T1, T2, T3> callback,
|
||||
T1 t1, T2 t2, T3 t3) : base(delay, interval, count)
|
||||
{
|
||||
Callback = callback;
|
||||
m_T1 = t1;
|
||||
m_T2 = t2;
|
||||
m_T3 = t3;
|
||||
|
||||
RegCreation();
|
||||
}
|
||||
|
||||
public TimerStateCallback<T1, T2, T3> Callback { get; }
|
||||
|
||||
public override bool DefRegCreation => false;
|
||||
|
||||
protected override void OnTick()
|
||||
{
|
||||
Callback?.Invoke(m_T1, m_T2, m_T3);
|
||||
}
|
||||
|
||||
public override string ToString() => $"DelayStateCall[{FormatDelegate(Callback)}]";
|
||||
}
|
||||
|
||||
public static Timer DelayCall<T1, T2, T3, T4>(TimerStateCallback<T1, T2, T3, T4> callback, T1 t1, T2 t2, T3 t3, T4 t4) =>
|
||||
DelayCall(TimeSpan.Zero, TimeSpan.Zero, 1, callback, t1, t2, t3, t4);
|
||||
|
||||
public static Timer DelayCall<T1, T2, T3, T4>(TimeSpan delay, TimerStateCallback<T1, T2, T3, T4> callback,
|
||||
T1 t1, T2 t2, T3 t3, T4 t4) => DelayCall(delay, TimeSpan.Zero, 1, callback, t1, t2, t3, t4);
|
||||
|
||||
public static Timer DelayCall<T1, T2, T3, T4>(TimeSpan delay, TimeSpan interval,
|
||||
TimerStateCallback<T1, T2, T3, T4> callback, T1 t1, T2 t2, T3 t3, T4 t4) =>
|
||||
DelayCall(delay, interval, 0, callback, t1, t2, t3, t4);
|
||||
|
||||
public static Timer DelayCall<T1, T2, T3, T4>(TimeSpan delay, TimeSpan interval, int count,
|
||||
TimerStateCallback<T1, T2, T3, T4> callback, T1 t1, T2 t2, T3 t3, T4 t4)
|
||||
{
|
||||
Timer t = new DelayStateCallTimer<T1, T2, T3, T4>(delay, interval, count, callback, t1, t2, t3, t4);
|
||||
|
||||
t.Priority = ComputePriority(count == 1 ? delay : interval);
|
||||
|
||||
t.Start();
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
private class DelayStateCallTimer<T1, T2, T3, T4> : Timer
|
||||
{
|
||||
private readonly T1 m_T1;
|
||||
private readonly T2 m_T2;
|
||||
private readonly T3 m_T3;
|
||||
private readonly T4 m_T4;
|
||||
|
||||
public DelayStateCallTimer(TimeSpan delay, TimeSpan interval, int count, TimerStateCallback<T1, T2, T3, T4> callback,
|
||||
T1 t1, T2 t2, T3 t3, T4 t4) : base(delay, interval, count)
|
||||
{
|
||||
Callback = callback;
|
||||
m_T1 = t1;
|
||||
m_T2 = t2;
|
||||
m_T3 = t3;
|
||||
m_T4 = t4;
|
||||
|
||||
RegCreation();
|
||||
}
|
||||
|
||||
public TimerStateCallback<T1, T2, T3, T4> Callback { get; }
|
||||
|
||||
public override bool DefRegCreation => false;
|
||||
|
||||
protected override void OnTick()
|
||||
{
|
||||
Callback?.Invoke(m_T1, m_T2, m_T3, m_T4);
|
||||
}
|
||||
|
||||
public override string ToString() => $"DelayStateCall[{FormatDelegate(Callback)}]";
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue