/// <summary> /// Retry a method that takes four parameters and returns a value of the type specified by the TResult parameter. /// </summary> /// <typeparam name="T1">The type of the first parameter of the action.</typeparam> /// <typeparam name="T2">The type of the secord parameter of the action.</typeparam> /// <typeparam name="T3">The type of the third parameter of the action.</typeparam> /// <typeparam name="T4">The type of the fourth parameter of the action.</typeparam> /// <typeparam name="TResult">The type of the return value.</typeparam> /// <param name="func">A method to retry.</param> /// <param name="t1">The first parameter of the action.</param> /// <param name="t2">The second parameter of the action.</param> /// <param name="t3">The third parameter of the action.</param> /// <param name="t4">The fourth parameter of the action.</param> /// <returns>The return value of the method.</returns> public TResult Retry <T1, T2, T3, T4, TResult>(RetryFunc <T1, T2, T3, T4, TResult> func, T1 t1, T2 t2, T3 t3, T4 t4) { TResult result = default(TResult); for (int i = 0; i < RetryCount; i++) { try { result = func(t1, t2, t3, t4); break; } catch { if (i == RetryCount - 1) { throw; } else { Thread.Sleep(TimeSpan.FromSeconds(IntervalSeconds)); } } } return(result); }
/// <summary> /// Retry a method that takes four parameters and returns a value of the type specified by the TResult parameter. /// </summary> /// <typeparam name="T1">The type of the first parameter of the action.</typeparam> /// <typeparam name="T2">The type of the secord parameter of the action.</typeparam> /// <typeparam name="T3">The type of the third parameter of the action.</typeparam> /// <typeparam name="T4">The type of the fourth parameter of the action.</typeparam> /// <typeparam name="TResult">The type of the return value.</typeparam> /// <param name="func">A method to retry.</param> /// <param name="t1">The first parameter of the action.</param> /// <param name="t2">The second parameter of the action.</param> /// <param name="t3">The third parameter of the action.</param> /// <param name="t4">The fourth parameter of the action.</param> /// <param name="match">Whether the return value is expected</param> /// <returns>The return value of the method.</returns> public TResult Retry <T1, T2, T3, T4, TResult>(RetryFunc <T1, T2, T3, T4, TResult> func, T1 t1, T2 t2, T3 t3, T4 t4, Predicate <TResult> match) { TResult result = default(TResult); for (int i = 0; i < RetryCount; i++) { try { result = func(t1, t2, t3, t4); if (!match(result)) { throw new RetryUnExpectedException(); } break; } catch { if (i == RetryCount - 1) { throw; } else { Thread.Sleep(TimeSpan.FromSeconds(IntervalSeconds)); } } } return(result); }
private T Retry <T>(RetryFunc <T> func, ReadOnlySpan <byte> data, CancellationToken cancellationToken = default) { for (int i = 0; i < this.attempts; ++i) { try { cancellationToken.ThrowIfCancellationRequested(); return(func(data)); } catch (DqliteException ex) when(ex.ErrorCode == 5) { cancellationToken.WaitHandle.WaitOne(this.delay); } } throw new DqliteException(5, "database is locked"); }
public static string Try(RetryFunc func, int interval = 500, int retyCount = 3) { int timeOutRetry = 3; int tryCount = 0; Exception lastException = null; do { tryCount++; try { var result = func(); if (result != null) { return result; } } catch (Exception ex) { if (ex.Message.Contains("The operation has timed out")) { //对于超时错误,多试几次。 if (--timeOutRetry > 0) { tryCount--; } continue; } } if (tryCount < retyCount) { System.Threading.Thread.Sleep(interval); } } while (tryCount < retyCount); throw new Exception(string.Format("Failed after {0} times tries, last error:{1}.", retyCount, lastException != null ? lastException.Message : string.Empty)); }
/// <summary> /// 从IMDomain获取商品及赠品的商品信息,主要是状态信息。 /// </summary> private void InitProductInfosFromIM(SaleGiftBatchInfo saleGiftBatchInfo) { List <int> productSysNos = new List <int>(); if (saleGiftBatchInfo.ProductItems1 != null) { foreach (ProductItemInfo item in saleGiftBatchInfo.ProductItems1) { productSysNos.Add(item.ProductSysNo.Value); } } if (saleGiftBatchInfo.ProductItems2 != null) { foreach (ProductItemInfo item in saleGiftBatchInfo.ProductItems2) { productSysNos.Add(item.ProductSysNo.Value); } } if (saleGiftBatchInfo.Gifts != null) { foreach (ProductItemInfo item in saleGiftBatchInfo.Gifts) { productSysNos.Add(item.ProductSysNo.Value); } } List <ProductInfo> products = ExternalDomainBroker.GetProductInfoListByProductSysNoList(productSysNos); //将商品的相关信息,状态、价格信息等赋值给Item. RetryFunc <ProductItemInfo, int> copyItemInfo = (ProductItemInfo item) => { ProductInfo swapItem = products.Find(p => p.SysNo == item.ProductSysNo); if (swapItem != null) { item.ProductStatus = swapItem.ProductStatus; item.ProductID = swapItem.ProductID; item.ProductName = swapItem.ProductName; item.CurrentPrice = swapItem.ProductPriceInfo.CurrentPrice.Value; item.C3SysNo = swapItem.ProductBasicInfo.ProductCategoryInfo.SysNo; item.BrandSysNo = swapItem.ProductBasicInfo.ProductBrandInfo.SysNo; } return(0); }; if (saleGiftBatchInfo.ProductItems1 != null) { foreach (ProductItemInfo item in saleGiftBatchInfo.ProductItems1) { copyItemInfo(item); } } if (saleGiftBatchInfo.ProductItems2 != null) { foreach (ProductItemInfo item in saleGiftBatchInfo.ProductItems2) { copyItemInfo(item); } } if (saleGiftBatchInfo.Gifts != null) { foreach (ProductItemInfo item in saleGiftBatchInfo.Gifts) { copyItemInfo(item); } } }