C# 如何在While循环条件不满足c之后调用函数#

C# 如何在While循环条件不满足c之后调用函数#,c#,while-loop,C#,While Loop,我有一个函数,它使用while循环并在条件满足时继续获取数据 public static void RetrieveData() { //if datetime in db is earlier then systemdatetime.now while (CompareDate.CompareDateTime() < 0) { Common.GetLastTime(); //while timespan of datetime i

我有一个函数,它使用while循环并在条件满足时继续获取数据

public static void RetrieveData()
{
    //if datetime in db is earlier then systemdatetime.now 
    while (CompareDate.CompareDateTime() < 0)
    {
        Common.GetLastTime();
        //while timespan of datetime is more then 10 minutes, so stops 10 minute before system time
        if (CompareDate.CompareTimespan() > 0)
        {
            DownloadData.GetFromApi("devicetypes", 
                                    "7654321234", 
                                    CreateCommand.BCurrentTimetoUnix(), 
                                    CreateCommand.SCurrentTimetoUnix());
        }
        //while timespan of datetime is =10 minutes, 
        else if (CompareDate.CompareTimespan() == 0)
        {
            DownloadData.GetFromApi("devicetypes", 
                                    "7654321234", 
                                    CreateCommand.BCurrentTimetoUnix(), 
                                    CreateCommand.SCurrentTimetoUnix());
        }
    }
    //if condition is not met run this
    DownloadData.GetFromApi("devicetypes", 
                            "7654321234", 
                            CreateCommand.BCurrentTimetoUnix(), 
                            CreateCommand.SCurrentTimetoUnix());
}
运行此命令一次,然后程序结束

   DownloadData.GetFromApi("devicetypes", 
                           "7654321234", 
                           CreateCommand.BCurrentTimetoUnix(), 
                           CreateCommand.SCurrentTimetoUnix());

我不知道你在追求什么,但我觉得添加
返回if
s中的
GetFromApi
之后,可以执行您想要的操作

if (CompareDate.CompareTimespan() > 0)
{
  DownloadData.GetFromApi(...);
  return; <---- 
}
 if (CompareDate.CompareTimespan() > 0)
{
   DownloadData.GetFromApi(...);
  return; <----
}
if(CompareDate.CompareTimespan()>0)
{
下载数据。GetFromApi(…);
返回;0)
{
下载数据。GetFromApi(…);

return;您当前的方法将执行所需的操作,或者您的意思是重复循环,当条件满足时,执行while循环中的操作,当条件不满足时,调用DownloadData方法?您有问题吗?当满足while条件时,以及当不满足while条件时,是否执行while循环中的操作在bot上运行该行汤姆@peeyushsingh@John我不确定它是否正确,这就是我提问的原因。也许像“是的,你的会有用”这样的回答会更好。无论如何,谢谢你花时间发表评论。我明白了。谢谢你澄清这一点。:-)返回的作用是什么?
返回
使函数“停止”“
break
将使
成为while
的“stop”。好的,我想让它做的是,如果while()中的条件满足,它将继续使用if语句,条件满足的任意一个语句。如果while()中的条件不满足它将只运行底部的代码行。在这种情况下,您的代码将已经这样做,或者在答案中选中选项2。
if (CompareDate.CompareTimespan() > 0)
{
  DownloadData.GetFromApi(...);
  return; <---- 
}
 if (CompareDate.CompareTimespan() > 0)
{
   DownloadData.GetFromApi(...);
  return; <----
}
if( CompareDate.CompareDateTime() >= 0) 
{
  DownloadData.GetFromApi("devicetypes", "7654321234",...)
}
else 
{
  while (CompareDate.CompareDateTime() < 0)
  {
     ...
  }

  // Nothing here
}