Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

Having trouble iterating through a stack for a "Simon" game. I created a random string generator to populate a stack to iterate through to trigger timers, and I can only get it to iterate the same index over and over. How do I get it to return to timer() and go to the next item in the stack (mySimon)? (without using Pop(), of course.) Cause right now, it just loops infinitely as you can surely see. To save space, I omitted the cases for each color and only included green because they are all identical. "green" refers to the actual button that flashes. timer() is actually called from a playback button each time, as well. Feel free to alter my code and re-post it with any suggestions. Thanks!

I should add that the interval of 1 millisecond is fine, because that's just the tick that's getting incremented. The actual blink I'm getting to happen is sufficient, my trouble is with getting it to loop properly.

 

C#
public Window1()
{
    InitializeComponent();
}
public void timer()
{
    foreach (string i in mySimon)
        if (isRunning == false)
        {
            switch (i)
            {
                case "myGreen":
                {
                    DispatcherTimer timer = new DispatcherTimer();
                    timer.Interval = TimeSpan.FromMilliseconds(1);
                    timer.Tick += new EventHandler(greenCount);
                    timer.Start();
                    isRunning = true;
                    Thread.Sleep(100);
                    break;
                }
            }
        }
}
public void greenCount(Object sender, EventArgs args)
{
    counter++;
    simonText.Text = counter.ToString();
    if (counter < 29)
    {
        green.Background = Brushes.LawnGreen;
    }
    if (counter > 29)
    {
        DispatcherTimer myTimer = (DispatcherTimer)sender;
        myTimer.Stop();
        counter = 0;
        green.Background = Brushes.Green;
        isRunning = false;
        timer();
    }
}
Posted
Updated 25-Nov-09 13:14pm
v12

timer.Interval = TimeSpan.FromMilliseconds(1);

 

This is insane.  You think you can see one millisecond ? 30 milliseconds is WAY too short.  And why have a timer if you're going to use a counter inside it ? Why not set the timer to 500 milliseconds and just swap every time ?

 
Share this answer
 
You are setting isRunning to true after the first iteration. Since the if statement inside your for loop checks isRunning, it will skip over all but the first item in the for loop.

When you say timer.Start(), the greenCount() method will never actually be reached until the timer() method has completed (that's how DispatcherTimers work... they only run on the UI thread).

Not sure what you are trying to accomplish here, but surely this is not the way to do it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900