public TimeOutModel(int id, int userId, long time, TimeOutDelegate td) { this.Id = id; this.Time = time; this.timeOutDelegate = td; this.userId = userId; }
//添加定时任务 XX时间后执行 public void AddTimeEvent(long delayTime, int userId, TimeOutDelegate timeOutDelegate) { Console.WriteLine("add timeout event " + userId); TimeOutModel model = new TimeOutModel(id.Add_Get(), userId, DateTime.Now.Ticks + delayTime, timeOutDelegate); idModelDict.TryAdd(model.Id, model); }
//添加定时任务 XX:XX:XX执行 public void AddTimerEvent(DateTime dateTime, int userId, TimeOutDelegate timeOutDelegate) { long delayTime = dateTime.Ticks - DateTime.Now.Ticks; if (delayTime <= 0) { return; } AddTimeEvent(delayTime, userId, timeOutDelegate); }
/// <summary> /// Execute a method with timeout check /// </summary> /// <typeparam name="T">Target method parameter type</typeparam> /// <typeparam name="TR">The result type of execution</typeparam> /// <param name="timeoutMethod">Target method</param> /// <param name="param">Target method parameter</param> /// <param name="result">The result of execution</param> /// <param name="timeout">Set timeout length</param> /// <returns>Is timeout</returns> public static Boolean Execute <T, TR>( TimeOutDelegate <T, TR> timeoutMethod, T param, out TR result, TimeSpan timeout) { var asyncResult = timeoutMethod.BeginInvoke(param, null, null); if (!asyncResult.AsyncWaitHandle.WaitOne(timeout, false)) { result = default(TR); return(true); } result = timeoutMethod.EndInvoke(asyncResult); return(false); }
public void AddACKCheckEvent(ACKDelegate check_delegate, int timeOut = -1, string timeOutMsg = "", TimeOutDelegate timeout_delegate = null) { if (timeOut > 0) { System.Threading.Timer timer = new System.Threading.Timer( delegate(object state) { if (timeOutEvent != null) { timeOutEvent.Invoke(); } RemoveACKCheckEvent(); //mainForm.ShowMessageBox(timeOutMsg); }, null, timeOut, System.Threading.Timeout.Infinite); ackCheckEvent += delegate() { timer.Dispose(); }; } ackCheckEvent += check_delegate; timeOutEvent += timeout_delegate; }
public void RemoveACKCheckEvent() { ackCheckEvent = null; timeOutEvent = null; }