using log4net; namespace Service.Execute; public class Invoker { private static readonly ILog Log = LogManager.GetLogger(typeof(Invoker)); public static InvokeStatus Invoke(string name, int duration, int times, Func cancel, Func done, Action doAction, Action timeoutAction, bool isTimeOutExit) { int hvPwrOffTimes = 0; while (!done()) { Log.Info($"begin {name}"); Thread.Sleep(duration); if (cancel()) { Log.Info($" {name} canceled"); return InvokeStatus.Cancel; } try { doAction(); } catch (Exception e) { Log.Error($"{name}", e); } if (hvPwrOffTimes++ > times) { timeoutAction(); Log.Info($" {name} timeout"); if (isTimeOutExit) { return InvokeStatus.TimeOut; } } } Log.Info($" {name} done"); return InvokeStatus.Done; } public static InvokeStatus Invoke(string name, int duration, int times, Func cancel, Func done, Action doAction, Action exceptionAction) { int count = 0; while (!done()) { Log.Info($"begin {name}"); Thread.Sleep(duration); if (cancel()) { Log.Info($" {name} canceled"); return InvokeStatus.Cancel; } try { doAction(); } catch (Exception e) { Log.Error($"{name}", e); exceptionAction(); } } Log.Info($" {name} done"); return InvokeStatus.Done; } public static InvokeStatus Invoke(string name, int duration, int times, Func cancel, Func done, Action doAction, Action timeoutAction, bool isTimeOutExit, Action exceptionAction, int timeOutActionTime, InvokeStatus timeOutException) { int count = 0; while (!done()) { Log.Info($"begin {name}"); Thread.Sleep(duration); if (cancel()) { Log.Info($" {name} canceled"); return InvokeStatus.Cancel; } try { doAction(); } catch (Exception e) { Log.Error($"{name}", e); exceptionAction(); } if (count++ > times) { if (count % timeOutActionTime == 0) { timeoutAction(); Log.Info($" {name} timeout"); if (isTimeOutExit) { return timeOutException; } } } } Log.Info($" {name} done"); return InvokeStatus.Done; } public static InvokeStatus Invoke(string name, int duration, int times, Func cancel,Func succ, Func done, Action doAction, Action timeoutAction, bool isTimeOutExit, Action exceptionAction, int timeOutActionTime, InvokeStatus timeOutException) { int count = 0; while (!done()) { Log.Info($"begin {name}"); Thread.Sleep(duration); if (cancel()) { Log.Info($" {name} canceled"); return InvokeStatus.Cancel; } if (succ()) { Log.Info($" {name} ManualSucc"); return InvokeStatus.ManualSucc; } try { doAction(); } catch (Exception e) { Log.Error($"{name}", e); exceptionAction(); } if (count++ > times) { if (count % timeOutActionTime == 0) { timeoutAction(); Log.Info($" {name} timeout"); if (isTimeOutExit) { return timeOutException; } } } } Log.Info($" {name} done"); return InvokeStatus.Done; } public static InvokeStatus Invoke(string name, int duration, int times, Func done, Action doAction, Action timeoutAction, bool isExcepOutExit, Action exceptionAction, int timeOutActionTime, InvokeStatus excepOutException) { int count = 0; while (!done()) { Log.Info($"begin {name}"); Thread.Sleep(duration); try { doAction(); } catch (Exception e) { Log.Error($"{name}", e); exceptionAction(); if (isExcepOutExit) { return excepOutException; } } if (count++ > times) { if (count % timeOutActionTime == 0) { timeoutAction(); Log.Info($" {name} timeout"); } } } Log.Info($" {name} done"); return InvokeStatus.Done; } public static InvokeStatus Invoke(string name, int duration, int times, Func done, Action doAction, Action timeoutAction, bool isTimeOut, Action exceptionAction, InvokeStatus timeOutActionTime) { int count = 0; while (!done()) { Log.Info($"begin {name}"); Thread.Sleep(duration); try { doAction(); } catch (Exception e) { Log.Error($"{name}", e); exceptionAction(); } if (count++ > times) { Log.Info($" {name} timeout"); timeoutAction(); if (isTimeOut) { return timeOutActionTime; } } } Log.Info($" {name} done"); return InvokeStatus.Done; } public static InvokeStatus Invoke(string name, int duration, int times, Func cancel, Func done, Action doAction) { int hvPwrOffTimes = 0; while (!done()) { Log.Info($"begin {name}"); Thread.Sleep(duration); if (cancel()) { Log.Info($" {name} canceled"); return InvokeStatus.Cancel; } try { doAction(); } catch (Exception e) { Log.Error($"{name}", e); } } Log.Info($" {name} done"); return InvokeStatus.Done; } } public enum InvokeStatus { Cancel, TimeOut, Done, Exception, None, ManualSucc, Fail, }