打赏

方法执行失败,重复执行指定次数某个方法

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();
            }
        }
    }

  执行结果:

 

posted @ 2018-08-14 17:21  刘奇云  阅读(381)  评论(0编辑  收藏  举报