CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2011
    Posts
    6

    Using a timer to go through a for loop.

    Hi there,

    I'm trying to figure out how I would get a for loop to run after a timer has elapsed. So when the timer elapses it circles through the for loop once, then the timer starts again and then goes through when it finish again until the required value has been achieved.

    I'm using this with a speed variable so that the speed decreases incrementally rather than instantly as at the moment the for loops seems to go from 10-1 too fast.

    public void time(object source, ElapsedEventArgs e)
    {
    timeB = true;
    SlowTimer.Interval = 500;
    SlowTimer.Enabled = true;
    }

    public void decceleration(object source, ElapsedEventArgs e)
    {
    SlowTimer.Interval = 500; // SlowTimer is the defined out of the scope.
    SlowTimer.Enabled = true;
    SlowTimer.AutoReset = false;
    SlowTimer.Elapsed += new ElapsedEventHandler(time);
    int[] slowSpeed = new int[10] {10,9,8,7,6,5,4,3,2,1 }; // new array 1-10

    if(timeB == true && SlowTimer.Enabled == true)
    {

    for (int i = 0; i < 10; i++) // for loop
    {
    int speed = 0;
    speed = slowSpeed[i];
    mSpeed = speed; // sets mSpeed to the value of i in the dashSpeed array

    timeB = false;

    if (mSpeed == 1)
    {
    horizontalSwimBehaviour(); // changes it back to the normal swimming
    rand = 1;
    }
    }
    }
    }

    Thanks.

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Using a timer to go through a for loop.

    If I look well, you never wait for the timer.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Nov 2011
    Posts
    6

    Re: Using a timer to go through a for loop.

    Elaborate? Wait for the timer?

  4. #4
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: Using a timer to go through a for loop.

    You are checking if the timer has finished but with an 'if' instead of a 'while'. I don't know how you are calling the decceleration method, but if you ask for timeB == true and the result is false, the method will just return with no effect, instead of waiting until the result is true.
    Doing that with a while would not be the best solution anyway, since it would be very consuming.

    Not sure what it is you want, but what you could do is just insert your code inside the timer callback and have it cycle through your array every time it elapses.

    Code:
    int i;
    int[] slowSpeed = new int[10] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; // new array 1-10
    
    public void time(object source, ElapsedEventArgs e)
    {
    	if (slowSpeed[i++] == 1)
    	{
    		horizontalSwimBehaviour(); // changes it back to the normal swimming
    		rand = 1;
    	}
    
    	if (i >= 10)
    	{
    		SlowTimer.Enabled = false; // disable the timer after all items have been checked
    		i = 0; // reset i
    	}
    }
    
    public void decceleration(object source, ElapsedEventArgs e)
    {
    	i = 0;
    
    	SlowTimer.Interval = 500; // SlowTimer is the defined out of the scope.
    	SlowTimer.AutoReset = true;
    	SlowTimer.Elapsed += new ElapsedEventHandler(time);
    
    	SlowTimer.Enabled = true; // enable after setting it up.
    	// Start the 'for' loop.
    	// To stop the loop just disable the timer.
    }

  5. #5
    Join Date
    Nov 2011
    Posts
    6

    Re: Using a timer to go through a for loop.

    So what I want to do is to decrease the speed of an object by 1 everytime the timer elapses, so everytime it goes through a different element of the array it changes the speed value to that.
    Thus it going 10-1, speed starts at 10 and ends up at 1.

    Been playing around with it and I can't seem to figure out where to put that the "i" within the array is the speed of the object.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Using a timer to go through a for loop.

    Why not call the code from the timer elapsed event and use a running counter to keep track of how many times if has cycled. Would seem to be easier and use less CPU cycles at runtime.
    Always use [code][/code] tags when posting code.

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

Featured