示例#1
0
    public static void RetryIf(int attempts, int Wait, CUtils.Workload work, Func <Exception, bool> cond)
    {
        Exception exception = (Exception)null;
        bool      flag      = true;
        int       num       = 0;

        while (flag)
        {
            if (num < attempts)
            {
                try
                {
                    work();
                    return;
                }
                catch (Exception ex)
                {
                    flag = cond(ex);
                    if (exception == null)
                    {
                        exception = ex;
                    }
                }
                Thread.Sleep(Wait);
                ++num;
            }
            else
            {
                break;
            }
        }
        throw exception;
    }
示例#2
0
 public static void TryLockRun(object lck, CUtils.Workload w)
 {
     if (!Monitor.TryEnter(lck))
     {
         return;
     }
     try
     {
         w();
     }
     finally
     {
         Monitor.Exit(lck);
     }
 }
示例#3
0
    public static void Retry(int attempts, int Wait, CUtils.Workload work)
    {
        Exception exception = (Exception)null;

        for (int index = 0; index < attempts; ++index)
        {
            try
            {
                work();
                return;
            }
            catch (Exception ex)
            {
                if (exception == null)
                {
                    exception = ex;
                }
            }
            Thread.Sleep(Wait);
        }
        throw exception;
    }
示例#4
0
    public static T Retry <T>(int attempts, int Wait, Func <T> work, CUtils.Workload reset)
    {
        Exception exception = (Exception)null;

        for (int index = 0; index < attempts; ++index)
        {
            try
            {
                return(work());
            }
            catch (Exception ex)
            {
                if (exception == null)
                {
                    exception = ex;
                }
            }
            reset();
            Thread.Sleep(Wait);
        }
        throw exception;
    }