添加项目文件。
This commit is contained in:
72
src/BRS.Common.Model/Helper/TimeOutHelper.cs
Normal file
72
src/BRS.Common.Model/Helper/TimeOutHelper.cs
Normal file
@ -0,0 +1,72 @@
|
||||
using System.Threading;
|
||||
|
||||
namespace BRS.Common.Model.Helper
|
||||
{
|
||||
public delegate void OnTimeoutDelegate();
|
||||
public class TimeOutHelper
|
||||
{
|
||||
private const int TIMEINTERVAL = 1000;
|
||||
private bool isPause = false;
|
||||
int timeTick = 0;
|
||||
|
||||
public event OnTimeoutDelegate OnTimeout;
|
||||
|
||||
public Timer MonitorTimer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 超时最大时间,以秒为单位
|
||||
/// </summary>
|
||||
public int MaxLimit { get; set; } = 250;
|
||||
|
||||
public TimeOutHelper()
|
||||
{
|
||||
InitialTimer();
|
||||
}
|
||||
|
||||
private void InitialTimer()
|
||||
{
|
||||
MonitorTimer = new Timer(new TimerCallback(OnTimeTick), null, Timeout.Infinite, TIMEINTERVAL);
|
||||
}
|
||||
|
||||
private void OnTimeTick(object state)
|
||||
{
|
||||
if (!isPause)
|
||||
{
|
||||
timeTick++;
|
||||
}
|
||||
|
||||
if (timeTick >= MaxLimit)
|
||||
{
|
||||
OnTimeout?.Invoke();
|
||||
Stop();
|
||||
}
|
||||
}
|
||||
|
||||
public TimeOutHelper(int limit)
|
||||
{
|
||||
MaxLimit = limit;
|
||||
InitialTimer();
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
timeTick = 0;
|
||||
MonitorTimer.Change(0, TIMEINTERVAL);
|
||||
}
|
||||
|
||||
public void Pause()
|
||||
{
|
||||
isPause = true;
|
||||
}
|
||||
|
||||
public void Resume()
|
||||
{
|
||||
isPause = false;
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
MonitorTimer.Change(Timeout.Infinite, Timeout.Infinite);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user