如果某些数据计数为零,如何重试执行一段代码

时间:2019-05-23 07:23:03

标签: c#

我有一个字符串列表,每隔一分钟用一个API调用填充

List<string> lst = new List<string>();

现在我需要在1分钟后重试方法PerformSomeAction中的代码,如果我的lst计数为零,该怎么做?

我可以使用Polly吗?

class Program
{
    static void Main(string[] args)
    {
        List<string> lst = new List<string>();

        if(lst.Count > 0)
        {
            //retry after one minute of lst.Count == 0
            PerformSomeAction();
        }
    }

    private static void PerformSomeAction()
    {
        Console.WriteLine("execute");
    }
}

1 个答案:

答案 0 :(得分:0)

这很容易实现:

while(lst.Count == 0)
{
  Thread.Sleep(1000*60);
  PerformAction();
}