class Program { static void Main(string[] args) { Test(); Console.ReadKey(); } static void Test() { var currentThreadId = Thread.CurrentThread.ManagedThreadId.ToString(); int tryTimes = 5; int connectTime = 0; while (true) { try { Console.WriteLine("currentThreadId:"+ currentThreadId+ "******connectTime:" + connectTime); Exception aa = null; string bb = aa.ToString(); return; } catch (Exception ex) { operateException(currentThreadId, tryTimes,ref connectTime); } } } static void operateException(string CurrentThreadId,int TryTimes,ref int ConnectTime) { if (TryTimes >= ConnectTime) { ConnectTime++; Thread.Sleep(2000); } else { throw new Exception(); } } }
执行结果: