4

I'm trying to figure out what should be the best method to do the following? I have a console application that works with httpwebrequest. In some part of my app I would like to do this:

  agent.GetURL("http://site.com/etc.../"); 
Wait for 8-16 Seconds<-Should be random  
 Agent.GetURL("http://site.com/etc.../");
Wait for 4-6 Seconds etc...

request are time sensitive so the computer time should be different from each request I can't just request wait for it done and request again. I have read around about timers, thread.sleep. I afraid I don't have experience with stopping or pausing Don't want to come up with problems in the future. So if you can advice me what best method will be for this kind of code?

And Is it possible the timer/thread.sleep to be inside a function and use it when ever I need?

Thanks!

Edit: This program is invisible so I need a solution with out loading circle mouse :)

Boris Daka
  • 603
  • 2
  • 17
  • 33

3 Answers3

7

Try

Random random = new Random();
int timeDelay = random.Next(8, 16);

Thread.Sleep(timeDelay * 1000); //time in milliseconds
Paul Grimshaw
  • 19,894
  • 6
  • 40
  • 59
  • This will not cause Freezing? I know if it's form app it will but how about Console? No Issues with this code it's safe to use (Reliable with the time?) – Boris Daka Sep 12 '12 at 09:43
  • Freezing? Not sure what you mean... the application will stop running for the duration of the timeDelay. – Paul Grimshaw Sep 12 '12 at 09:47
  • Well it will not show this circle mouse loading figure when it's waiting? That what I meant :) – Boris Daka Sep 12 '12 at 09:51
  • If you want it to run in the background, you will have to shift it off to a background worker thread. http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx – Paul Grimshaw Sep 12 '12 at 09:52
  • Not really sure how to implement the BG Worker as well, Will it be easy as calling a function or I will have to have If ... Then... I still would like to keep my code simple and clean :) – Boris Daka Sep 12 '12 at 10:06
  • @MarkVoidale Console applications do not freeze when Thread.Sleep is used. – Artemix Sep 12 '12 at 12:37
4

Use Thread.Sleep(miliseconds) to stop the execution for the desired time.

cuongle
  • 74,024
  • 28
  • 151
  • 206
Oscar
  • 13,594
  • 8
  • 47
  • 75
4

This question provides a method to generate random numbers in a defined range, you can take that as-is and wrap it in another method to pause for a random number of seconds.

Code could look like this:

// Method from the linked answer, copy-pasted here for completeness
// added the relevant fields because the original code is an override
public Int32 GetNewRandomNumber(Int32 minValue, Int32 maxValue)
{
    RNGCryptoServiceProvider _rng = new RNGCryptoServiceProvider();
    byte[] _uInt32Buffer = new byte[4];

    if (minValue > maxValue) 
        throw new ArgumentOutOfRangeException("minValue");
    if (minValue == maxValue) return minValue;
    Int64 diff = maxValue - minValue;
    while (true)
    {
        _rng.GetBytes(_uint32Buffer);
        UInt32 rand = BitConverter.ToUInt32(_uint32Buffer, 0);

        Int64 max = (1 + (Int64)UInt32.MaxValue);
        Int64 remainder = max % diff;
        if (rand < max - remainder)
        {
            return (Int32)(minValue + (rand % diff));
        }
    }
}

public void RandomWait(int minWait, int maxWait)
{
    // Thread.Sleep() wants a number of milliseconds to wait
    // We're going, as an example, to wait between 8 and 16 seconds
    System.Threading.Thread.Sleep(GetNewRandomNumber(8, 16) * 1000);
}

Then you just call RandomWait() when you need it.

Community
  • 1
  • 1
Alex
  • 23,004
  • 4
  • 39
  • 73
  • Thanks a lot! This does look great, Quick question this randomnumber code is different from the regular r.next(8,17)? And about Thread.Sleep This will not cause Freezing(Circle Mouse Loading?) And is it reliable with time? If it's small delay it's not too scary. – Boris Daka Sep 12 '12 at 09:49
  • `Thread.Sleep` simply stops the application for some time (being a console application, it will just seem to hang for a while). The difference with the regular `r.Next()` is that this one performs better in terms of randomization. The proposed `Random()` application would produce the exact same sequence of numbers every time you run your application (you may or may not desire it, nonetheless, this is "more random"). – Alex Sep 12 '12 at 10:22
  • Maybe I misread: if you *want* to show up a hint via mouse "circle" loading cursor, then this won't do: you're going to have to split behavior across multiple threads. I'd advise against it though: console applications don't usually give feedback through mouse/windows GUI. – Alex Sep 12 '12 at 10:25
  • Sounds good Alex thanks a lot! I will try it and let you know how it goes! :) – Boris Daka Sep 12 '12 at 12:35