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 :)

3 Answers 3

7

Try

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

Thread.Sleep(timeDelay * 1000); //time in milliseconds
6
  • 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, 2012 at 9:43
  • Freezing? Not sure what you mean... the application will stop running for the duration of the timeDelay. Sep 12, 2012 at 9:47
  • Well it will not show this circle mouse loading figure when it's waiting? That what I meant :)
    – Boris Daka
    Sep 12, 2012 at 9:51
  • If you want it to run in the background, you will have to shift it off to a background worker thread. msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx Sep 12, 2012 at 9: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, 2012 at 10:06
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.

4
  • 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, 2012 at 9: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, 2012 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, 2012 at 10:25
  • Sounds good Alex thanks a lot! I will try it and let you know how it goes! :)
    – Boris Daka
    Sep 12, 2012 at 12:35
4

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

1
  • Perfect solution :)
    – Earth
    Feb 15, 2022 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.