Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.

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.

 

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 25-Nov-09 12:46pm
Edited 25-Nov-09 14:14pm
DaveyM6977.8K
v12
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 ?

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 555
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 359
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 25 Nov 2009
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100