@#¥%……

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

public delegate void AnonymousHandler();

/// <summary>
/// 重试某过程 maxError 次,直到成功或失败
/// </summary>
/// <param name="handler">托管函数</param>
/// <param name="maxError">允许失败的次数</param>
/// <returns>如果执行成功,则返回 null, 否则返回该错误对象</returns>
public static Exception Trys(AnonymousHandler handler, int maxError) {
if (handler != null) {
Exception ex = null;
for (int a = 0; a < maxError; a++) {
try {
handler();
return null;
} catch (Exception e) {
ex = e;
}
}
return ex;
}
return null;
}

ex = Lib.Trys(delegate()
{
ie.Action = url;
ie.CookieContainer = new CookieContainer();
ie.AcceptLanguage = "zh-cn,zh;q=0.5";
ie.CookieContainer.SetCookies(ie.Address, "Culture=zh-hk");
ie.Send();
Thread.CurrentThread.Join(100);
}, 10);

posted on 2014-11-25 21:08  yufan27209  阅读(710)  评论(0编辑  收藏  举报