|
|
|
@ -6,14 +6,12 @@ using System.Threading.Tasks; |
|
|
|
|
using System.Timers; |
|
|
|
|
using Timer = System.Timers.Timer; |
|
|
|
|
|
|
|
|
|
namespace Soul2.General.utils |
|
|
|
|
{ |
|
|
|
|
namespace Soul2.General.utils { |
|
|
|
|
/// <summary> |
|
|
|
|
/// 扩展System.Timers.Timer |
|
|
|
|
/// By Soul2 |
|
|
|
|
/// </summary> |
|
|
|
|
public static class TimerUtils |
|
|
|
|
{ |
|
|
|
|
public static class TimerUtils { |
|
|
|
|
private const int default_loop_times = 512; |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
@ -24,17 +22,14 @@ namespace Soul2.General.utils |
|
|
|
|
/// <param name="time"></param> |
|
|
|
|
/// <param name="callback"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
public static Timer startOnce(this Timer timer, double time, Action<Timer> callback) |
|
|
|
|
{ |
|
|
|
|
void onElapsed(object source, ElapsedEventArgs e) |
|
|
|
|
{ |
|
|
|
|
public static Timer startOnce(this Timer timer, double time, Action<Timer> callback) { |
|
|
|
|
void onElapsed(object source, ElapsedEventArgs e) { |
|
|
|
|
callback(timer); |
|
|
|
|
timer.destroy(); |
|
|
|
|
} |
|
|
|
|
return run(timer, (int)(time * 1000), false, onElapsed); |
|
|
|
|
} |
|
|
|
|
public static Timer startOnce(this Timer timer, double time, Action callback) |
|
|
|
|
{ |
|
|
|
|
public static Timer startOnce(this Timer timer, double time, Action callback) { |
|
|
|
|
return timer.startOnce(time, (t) => callback?.Invoke()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -48,36 +43,28 @@ namespace Soul2.General.utils |
|
|
|
|
/// <param name="times">次数</param> |
|
|
|
|
/// <param name="callback"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
public static Timer startLoop(this Timer timer, double time, int times, Action<Timer, int> callback) |
|
|
|
|
{ |
|
|
|
|
public static Timer startLoop(this Timer timer, double time, int times, Action<Timer, int> callback) { |
|
|
|
|
int run_times = 0; |
|
|
|
|
void onElapsed(object source, ElapsedEventArgs e) |
|
|
|
|
{ |
|
|
|
|
void onElapsed(object source, ElapsedEventArgs e) { |
|
|
|
|
run_times += 1; |
|
|
|
|
if (run_times <= times) |
|
|
|
|
{ |
|
|
|
|
if (run_times <= times) { |
|
|
|
|
callback(timer, run_times); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
} else { |
|
|
|
|
timer.destroy(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return run(timer, (int)(time * 1000), true, onElapsed); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Timer startLoop(this Timer timer, double time, int times, Action<Timer> callback) |
|
|
|
|
{ |
|
|
|
|
public static Timer startLoop(this Timer timer, double time, int times, Action<Timer> callback) { |
|
|
|
|
return timer.startLoop(time, times, (t, i) => callback?.Invoke(t)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Timer startLoop(this Timer timer, double time, int times, Action<int> callback) |
|
|
|
|
{ |
|
|
|
|
public static Timer startLoop(this Timer timer, double time, int times, Action<int> callback) { |
|
|
|
|
return timer.startLoop(time, times, (t, i) => callback?.Invoke(i)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Timer startLoop(this Timer timer, double time, int times, Action callback) |
|
|
|
|
{ |
|
|
|
|
public static Timer startLoop(this Timer timer, double time, int times, Action callback) { |
|
|
|
|
return timer.startLoop(time, times, (t, i) => callback?.Invoke()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -90,40 +77,30 @@ namespace Soul2.General.utils |
|
|
|
|
/// <param name="times">次数</param> |
|
|
|
|
/// <param name="callback"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
public static Timer startKeep(this Timer timer, double time, Action<Timer, int> callback, int times = default_loop_times) |
|
|
|
|
{ |
|
|
|
|
public static Timer startKeep(this Timer timer, double time, Action<Timer, int> callback, int times = default_loop_times) { |
|
|
|
|
int run_times = 0; |
|
|
|
|
void onElapsed(object source, ElapsedEventArgs e) |
|
|
|
|
{ |
|
|
|
|
void onElapsed(object source, ElapsedEventArgs e) { |
|
|
|
|
run_times += 1; |
|
|
|
|
if (run_times <= times) |
|
|
|
|
{ |
|
|
|
|
if (run_times <= times) { |
|
|
|
|
callback(timer, run_times); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
} else { |
|
|
|
|
timer.destroy(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return run(timer, (int)(time * 1000), true, onElapsed); |
|
|
|
|
} |
|
|
|
|
public static Timer startKeep(this Timer timer, double time, Action<int> callback, int times = default_loop_times) |
|
|
|
|
{ |
|
|
|
|
public static Timer startKeep(this Timer timer, double time, Action<int> callback, int times = default_loop_times) { |
|
|
|
|
return timer.startKeep(time, (t, i) => callback?.Invoke(i)); |
|
|
|
|
} |
|
|
|
|
public static Timer startKeep(this Timer timer, double time, Action<Timer> callback, int times = default_loop_times) |
|
|
|
|
{ |
|
|
|
|
public static Timer startKeep(this Timer timer, double time, Action<Timer> callback, int times = default_loop_times) { |
|
|
|
|
return timer.startKeep(time, (t, i) => callback?.Invoke(t)); |
|
|
|
|
} |
|
|
|
|
public static Timer startKeep(this Timer timer, double time, Action callback, int times = default_loop_times) |
|
|
|
|
{ |
|
|
|
|
public static Timer startKeep(this Timer timer, double time, Action callback, int times = default_loop_times) { |
|
|
|
|
return timer.startKeep(time, (t, i) => callback?.Invoke()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void destroy(this Timer timer) |
|
|
|
|
{ |
|
|
|
|
if (timer.Enabled) |
|
|
|
|
{ |
|
|
|
|
public static void destroy(this Timer timer) { |
|
|
|
|
if (timer.Enabled) { |
|
|
|
|
timer.Stop(); |
|
|
|
|
} |
|
|
|
|
timer.Dispose(); |
|
|
|
@ -131,8 +108,7 @@ namespace Soul2.General.utils |
|
|
|
|
|
|
|
|
|
// ----------------------------------------- |
|
|
|
|
|
|
|
|
|
private static Timer run(Timer timer, int interval, bool loop, ElapsedEventHandler elapsed) |
|
|
|
|
{ |
|
|
|
|
private static Timer run(Timer timer, int interval, bool loop, ElapsedEventHandler elapsed) { |
|
|
|
|
timer.Elapsed += elapsed; |
|
|
|
|
timer.Interval = interval; |
|
|
|
|
timer.AutoReset = loop; |
|
|
|
@ -140,50 +116,40 @@ namespace Soul2.General.utils |
|
|
|
|
return timer; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Timer startOnce(double time, Action<Timer> callback) |
|
|
|
|
{ |
|
|
|
|
public static Timer startOnce(double time, Action<Timer> callback) { |
|
|
|
|
return new Timer().startOnce(time, callback); |
|
|
|
|
} |
|
|
|
|
public static Timer startOnce(double time, Action callback) |
|
|
|
|
{ |
|
|
|
|
public static Timer startOnce(double time, Action callback) { |
|
|
|
|
return new Timer().startOnce(time, callback); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ----------------------------------------- |
|
|
|
|
|
|
|
|
|
public static Timer startLoop(double time, int times, Action<Timer, int> callback) |
|
|
|
|
{ |
|
|
|
|
public static Timer startLoop(double time, int times, Action<Timer, int> callback) { |
|
|
|
|
return new Timer().startLoop(time, times, callback); |
|
|
|
|
} |
|
|
|
|
public static Timer startLoop(double time, int times, Action<Timer> callback) |
|
|
|
|
{ |
|
|
|
|
public static Timer startLoop(double time, int times, Action<Timer> callback) { |
|
|
|
|
return new Timer().startLoop(time, times, callback); |
|
|
|
|
} |
|
|
|
|
public static Timer startLoop(double time, int times, Action<int> callback) |
|
|
|
|
{ |
|
|
|
|
public static Timer startLoop(double time, int times, Action<int> callback) { |
|
|
|
|
return new Timer().startLoop(time, times, callback); |
|
|
|
|
} |
|
|
|
|
public static Timer startLoop(double time, int times, Action callback) |
|
|
|
|
{ |
|
|
|
|
public static Timer startLoop(double time, int times, Action callback) { |
|
|
|
|
return new Timer().startLoop(time, times, callback); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ----------------------------------------- |
|
|
|
|
|
|
|
|
|
public static Timer startKeep(double time, Action<Timer, int> callback, int times = default_loop_times) |
|
|
|
|
{ |
|
|
|
|
public static Timer startKeep(double time, Action<Timer, int> callback, int times = default_loop_times) { |
|
|
|
|
return new Timer().startKeep(time, callback, times); |
|
|
|
|
} |
|
|
|
|
public static Timer startKeep(double time, Action<int> callback, int times = default_loop_times) |
|
|
|
|
{ |
|
|
|
|
public static Timer startKeep(double time, Action<int> callback, int times = default_loop_times) { |
|
|
|
|
return new Timer().startKeep(time, callback, times); |
|
|
|
|
} |
|
|
|
|
public static Timer startKeep(double time, Action<Timer> callback, int times = default_loop_times) |
|
|
|
|
{ |
|
|
|
|
public static Timer startKeep(double time, Action<Timer> callback, int times = default_loop_times) { |
|
|
|
|
return new Timer().startKeep(time, callback, times); |
|
|
|
|
} |
|
|
|
|
public static Timer startKeep(double time, Action callback, int times = default_loop_times) |
|
|
|
|
{ |
|
|
|
|
public static Timer startKeep(double time, Action callback, int times = default_loop_times) { |
|
|
|
|
return new Timer().startKeep(time, callback, times); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|