Best asynchronous while method

前端 未结 4 2034
离开以前
离开以前 2020-12-28 14:57

I need to write some asynchronous code that essentially attempts to repeatedly talk to and initialise a database. Quite often the first attempt will fail hence the requirem

4条回答
  •  一向
    一向 (楼主)
    2020-12-28 15:30

    Just provide another solution

    public static void WaitForCondition(Func predict)
        {
            Task.Delay(TimeSpan.FromMilliseconds(1000)).ContinueWith(_ =>
            {
                var result = predict();
                // the condition result is false, and we need to wait again.
                if (result == false)
                {
                    WaitForCondition(predict);
                }
            });
        }
    

提交回复
热议问题