0

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?

    private static void Main()
    {
        SettingsComponent.LoadSettings();

        while (true)
        {
            try
            {
                    GenerateRandomBooking();
                    GenerateRandomBids();
                    AllocateBids();
                    Thread.Sleep(TimeSpan.FromSeconds(5));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
methuselah
  • 12,766
  • 47
  • 165
  • 315
  • 1
    did you try `Thread.Sleep(msec)`? and once you ask (*what is the best way*), you should at least provide one way. + it is almost every time opinion based – chouaib Feb 10 '15 at 08:48

2 Answers2

3

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

    }
}
Kavindu Dodanduwa
  • 12,193
  • 3
  • 33
  • 46
2

using Thread.Sleep() is almost always a bad idea. 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());
        }

    }
}  
apomene
  • 14,282
  • 9
  • 46
  • 72
  • Why is it always a bad idea? What could happen? – methuselah Feb 10 '15 at 09:17
  • 1
    Check this: http://stackoverflow.com/questions/8815895/why-is-thread-sleep-so-harmful – apomene Feb 10 '15 at 09:23
  • 1
    What goes in between the `while (timer.Enabled) { ///wait time equal to timer interval... }`, its the one part of your code that I don't get – methuselah Feb 10 '15 at 09:59
  • 1
    you put nothing..it is there to get the proccess idle for 2 sec – apomene Feb 10 '15 at 10:00
  • Thanks - ok I'm incorporating your code in, but I keep getting a `cannot resolve symbol` error. Does timer have to be put into the Main() method or can it be placed in the `internal class`? Code here: http://pastebin.com/FtvP3PWS – methuselah Feb 10 '15 at 10:07
  • `while(something){ };` will probably freeze the app, it is most probably not the **BEST** way by far – chouaib Feb 10 '15 at 10:26