using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Aitex.Core.Utilities
{
///
/// 用于简化多次尝试执行动作,仅判断一次成功或者错误.类似于Trigger,但是增加了上升沿,下降沿两种逻辑
///
public class Retry
{
///
/// 第一次触发成功
///
public bool IsSucceeded { get; set; }
///
/// 第一次触发失败
///
public bool IsErrored { get; set; }
bool _result = false;
int _retryTime = -1;
///
/// 当前执行结果
///
public bool Result
{
set
{
if (value)
{
IsSucceeded = !_result;
IsErrored = false;
}
else
{
IsErrored = (_result || _retryTime == -1);
IsSucceeded = false;
}
_result = value;
_retryTime = ++_retryTime % 100;
}
get
{
return _result;
}
}
public Retry()
{
}
public void Do(Func function, int time)
{
for (int i = 0; i < time; i++)
{
if (function())
Result = true;
Thread.Sleep(10);
}
Result = false;
}
}
}