简体   繁体   English

多次运行方法,并延迟几秒钟

[英]Run method several times and delay one by a few seconds

What would be the best way of calling the GenerateRandomBooking() method 5 times and then delaying the GenerateRandomBids() by 2-3 seconds in the while loop below of my console application? 在控制台应用程序下面的while循环中,调用5次GenerateRandomBooking()方法然后将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());
            }
        }
    }

What about something like this 那这样的事呢

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

    }
}

using Thread.Sleep() is almost always a bad idea. 使用Thread.Sleep()几乎总是一个坏主意。 I believe it is better to use a timer: 我相信最好使用计时器:

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

    }
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多次更新 Textview 文本,延迟几秒钟 - Update Textview text multiple times with few seconds delay 为什么我运行方法一次完成工作几乎同时如果我在for循环c中运行几次# - Why if I run method one time is done work almost the same time if I run few times in for loop c# 使用计时器将操作延迟几秒钟 - Using Timer to delay an operation a few seconds 统一摧毁后如何重生坦克? (有几秒钟的延迟) - How to respawn the tank after being destroyed in unity? (with a few seconds delay) 延迟控制台应用程序几秒钟的最佳方法是什么 - What is the best way to delay a console application for a few seconds 有没有一种方法可以从一个方法多次调用不同的http请求,直到获得答案? - Is there a way to call different http-requests from one method several times until we get the answer? 使用线程延迟显示几个文本框 - Display few text boxes one by one with delay using Threads C#“字符串之间”运行多次 - C# "between strings" run several times 为什么在此代码中多次运行MessageBox? - Why does MessageBox be run several times in this code? 使用await与Task.Run,​​但UI仍然挂起几秒钟? - Using await with Task.Run but UI still hangs for a few seconds?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM