Results 1 to 3 of 3

Thread: [RESOLVED] Random Intervals

  1. #1

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Resolved [RESOLVED] Random Intervals

    Howdy all,

    I'm wanting to perform a task at random intervals until specfied otherwise. Basically, when I call a Start() function, it should call a DoSomething() function, until I call Stop(). My problem is a) Writing a "Gameloop" type thing to handle this, and b) generating the randomness. I can't have a set interval, because the program needs to be as random as can be whilst performing the task. It also can't perform this task, about, 2 minutes apart. So, if I called start now, the program would perform the task, then, it can't perform it for another 2 minutes at least.

    I've been thinking about it for a while and I'm not quite sure where to start in C#.

    Any help would be appreciated

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Random Intervals

    If you want to perform a task multiple times at an interval then you use a Timer. If you want to perform this task at random intervals then you use a Random object to generate a number and each time the Timer Ticks you set that number as the new Interval:
    Code:
    // The minimum interval is 2 minutes, expressed as milliseconds
    private const int MIN_INTERVAL = 2 * 60 * 1000;
    
    // The maximum interval is 5 minutes, expressed as milliseconds
    private const int MAX_INTERVAL = 5 * 60 * 1000;
    
    private Random intervalGenerator = new Random();
    
    private void StartPerformingTask()
    {
        // Set a new random interval for the task.
        this.SetInterval();
    
        // Start the timer that will invoke the task.
        this.timer1.Start();
    }
    
    private void StopPerformingTask()
    {
        // Stop the timer invoking the task.
        this.timer1.Stop();
    }
    
    private void SetInterval()
    {
        // Set a random inetrval on the timer.
        this.timer1.Interval = this.intervalGenerator.Next(MIN_INTERVAL, MAX_INTERVAL + 1);
    }
    
    private void PerformTask()
    {
        // Do something here.
    }
    
    private void timer1_Tick(object sender, EventArgs e)
    {
        // Set a new random interval for the task.
        this.SetInterval();
    
        // Perform the task now.
        this.PerformTask();
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Random Intervals

    Mmm, I knew about timers, I just wasn't sure if they're the best option, considering using them over long periods of time in pre-.NET (VB6 mainly) was horrible, as it became less and less accurate. These actions will be performed over 40 minutes on average, and upto an hour.

    Thanks for the info on the random number generator, I think this will be sufficient. Nice and helpful as always jmc

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width