protected void RedialInternet() { Redialer?.Redial(); NetworkValidater?.Wait(); Thread.Sleep(2000); }
public RedialResult Redial(Action action = null) { if (CheckIsRedialing()) { while (true) { Thread.Sleep(50); if (!CheckIsRedialing()) { //ReleaseRedialLocker(); return(RedialResult.OtherRedialed); } } } else { try { LogCenter.Log(null, "Wait all atomic action...", LogLevel.Warn); // 等待数据库等操作完成 WaitAll(); LogCenter.Log(null, "Start redial...", LogLevel.Warn); Redialer.Redial(); InternetDetector.Detect(); Thread.Sleep(2000); LogCenter.Log(null, "Redial finished.", LogLevel.Warn); action?.Invoke(); return(RedialResult.Sucess); } catch (IOException) { // 有极小可能同时调用File.Open时抛出异常 return(Redial(action)); } catch (Exception) { return(RedialResult.Failed); } finally { ReleaseRedialLocker(); if (AfterRedialWaitTime > 0) { Console.WriteLine($"Going to sleep for {AfterRedialWaitTime} after redial."); Thread.Sleep(AfterRedialWaitTime); } } } }
public RedialResult Redial() { if (CheckIsRedialing()) { while (true) { Thread.Sleep(50); if (!CheckIsRedialing()) { return(RedialResult.OtherRedialed); } } } else { try { Logger.Warn("Wait all atomic action..."); // 等待数据库等操作完成 WaitAll(); Logger.Warn("Start redial..."); Redialer.Redial(); InternetDetector.Detect(); Thread.Sleep(2000); Logger.Warn("Redial finished."); return(RedialResult.Sucess); } catch (IOException) { // 有极小可能同时调用File.Open时抛出异常 return(Redial()); } catch (Exception) { return(RedialResult.Failed); } finally { ReleaseRedialLocker(); } } }
protected void RedialInternet() { if (Redialer == null) { return; } Redialer.Redial(); if (NetworkValidater == null) { return; } while (!NetworkValidater.Wait()) { Redialer.Redial(); } Thread.Sleep(2000); }
/// <summary> /// 执行拨号 /// </summary> /// <param name="action">执行完拨号后回调方法</param> /// <returns>拨号结果</returns> public RedialResult Redial(Action <object> action = null) { ILocker locker = null; try { Logger.Default.Information("Try to lock redialer..."); locker = CreateLocker(); locker.Lock(); Logger.Default.Information("Lock redialer"); var lastRedialTime = (DateTime.Now - GetLastRedialTime()).Seconds; if (lastRedialTime < RedialIntervalLimit) { Logger.Default.Information($"Skip redial because last redial compeleted before {lastRedialTime} seconds."); return(RedialResult.OtherRedialed); } Thread.Sleep(500); Logger.Default.Information("Wait all network requests complete..."); // 等待所有网络请求结束 WaitAllNetworkRequestComplete(); Logger.Default.Information("Start redial..."); var redialCount = 1; while (redialCount++ < 10) { try { Console.WriteLine($"redial loop {redialCount}"); if (!IsTest) { Redialer.Redial(); } if (InternetDetector.Detect()) { Console.WriteLine($"redial loop {redialCount} success."); break; } else { Console.WriteLine($"redial loop {redialCount} failed."); } } catch (Exception ex) { Logger.Default.Error($"Redial failed loop {redialCount}: {ex}"); } } if (redialCount > 10) { return(RedialResult.Failed); } Logger.Default.Information("Redial success."); action?.Invoke(null); RecordLastRedialTime(); return(RedialResult.Sucess); } catch (Exception ex) { Logger.Default.Error($"Redial failed: {ex}"); return(RedialResult.Failed); } finally { locker?.Release(); if (SleepAfterRedial > 0) { Console.WriteLine($"Going to sleep for {SleepAfterRedial} after redial."); Thread.Sleep(SleepAfterRedial); } } }
public RedialResult Redial(Action action = null) { if (CheckIsRedialing()) { while (true) { Thread.Sleep(50); if (!CheckIsRedialing()) { //ReleaseRedialLocker(); return(RedialResult.OtherRedialed); } } } else { try { Logger.MyLog("Wait all atomic action...", LogLevel.Warn); // 等待数据库等操作完成 WaitAll(); Logger.MyLog("Start redial...", LogLevel.Warn); var redialCount = 0; while (redialCount++ < 10) { Console.WriteLine($"redial loop {redialCount}"); Redialer.Redial(); if (InternetDetector.Detect()) { Console.WriteLine($"redial loop {redialCount} success!!!!"); break; } else { Console.WriteLine($"redial loop {redialCount} failed!!!!"); } } if (redialCount > 10) { return(RedialResult.Failed); } Thread.Sleep(2000); Logger.MyLog("Redial finished.", LogLevel.Warn); action?.Invoke(); return(RedialResult.Sucess); } catch (IOException) { // 有极小可能同时调用File.Open时抛出异常 return(Redial(action)); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return(RedialResult.Failed); } finally { ReleaseRedialLocker(); if (AfterRedialWaitTime > 0) { Console.WriteLine($"Going to sleep for {AfterRedialWaitTime} after redial."); Thread.Sleep(AfterRedialWaitTime); } } } }
public RedialResult Redial(Action action = null) { try { Logger.MyLog("Try to lock redialer...", LogLevel.Warn); LockRedial(); Logger.MyLog("Lock redialer", LogLevel.Warn); var lastRedialTime = GetLastRedialTime(); if ((DateTime.Now - lastRedialTime).Seconds < RedialIntervalLimit) { return(RedialResult.OtherRedialed); } Thread.Sleep(500); Logger.MyLog("Wait all network requests complete...", LogLevel.Warn); // 等待所有网络请求结束 WaitAllNetworkRequestComplete(); Logger.MyLog("Start redial...", LogLevel.Warn); var redialCount = 1; while (redialCount++ < 10) { try { Console.WriteLine($"redial loop {redialCount}"); Redialer.Redial(); if (InternetDetector.Detect()) { Console.WriteLine($"redial loop {redialCount} success."); break; } else { Console.WriteLine($"redial loop {redialCount} failed."); } } catch (Exception ex) { Logger.MyLog($"Redial failed loop {redialCount}: {ex}", LogLevel.Error); continue; } } if (redialCount > 10) { return(RedialResult.Failed); } Logger.MyLog("Redial success.", LogLevel.Warn); action?.Invoke(); return(RedialResult.Sucess); } catch (Exception ex) { Logger.MyLog($"Redial failed: {ex}", LogLevel.Error); return(RedialResult.Failed); } finally { ReleaseRedialLock(); if (AfterRedialWaitTime > 0) { Console.WriteLine($"Going to sleep for {AfterRedialWaitTime} after redial."); Thread.Sleep(AfterRedialWaitTime); } } }