什么是调用GenerateRandomBooking()方法5次,然后在我的控制台应用程序的while循环中将GenerateRandomBids()延迟2-3秒的最佳方法?
private static void Main()
{
SettingsComponent.LoadSettings();
while (true)
{
try
{
GenerateRandomBooking();
GenerateRandomBids();
AllocateBids();
Thread.Sleep(TimeSpan.FromSeconds(5));
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}发布于 2015-02-10 08:52:35
像这样的东西怎么样
private static void Main()
{
SettingsComponent.LoadSettings();
while (true)
{
try
{
for(int x=0; x<4 ; x++){
GenerateRandomBooking(); // Will call 5 times
}
Thread.Sleep(2000) // 2 seconds sleep
GenerateRandomBids();
AllocateBids();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}发布于 2015-02-10 09:05:21
使用Thread.Sleep()几乎总是一个坏主意。我认为最好用计时器:
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 2000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled=false;
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer.Enabled=false;
}
private static void Main()
{
SettingsComponent.LoadSettings();
int counter =0;
while (true)
{
try
{
GenerateRandomBooking();
GenerateRandomBids();
AllocateBids();
counter ++;
if(counter > 4){
timer.Enabled=true;
while (timer.Enabled)
{
///wait time equal to timer interval...
}
counter=0;
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
} https://stackoverflow.com/questions/28427347
复制相似问题