1

Okay so this is my second time posting here so sorry if this question has already been answered, I couldnt find anything on it.

So basically I have a button, which I want to start a loop when its pressed, I then want the loop to keep repeating until the same button is pressed again, I've been trying to figure this out for a while and I cant seem to get this to work.

So Inside the loop, the program is changing the image inside several picture boxes, I want the program to continue changing the images in a loop until the same button is clicked

Thanks for your help :)

If you need any more information just let me know

EDIT:

    private void btnDance_Click(object sender, EventArgs e)
    {
        bool clicked;
        if (clicked == true)
        {
            timer1.Stop();
        }
        else
        {
            clicked = true;
            timer1.Start();
        }
    }
5
  • 2
    Share a relevant snippet of what you've done so far. Commented Nov 27, 2013 at 17:38
  • This isn't showing too much Commented Nov 27, 2013 at 17:39
  • 1
    Could use a BackgroundWorker and that would prevent the UI from "freezing" up. Commented Nov 27, 2013 at 17:40
  • 2
    I think the real question is: Why does my application seem to freeze when I start a loop? Commented Nov 27, 2013 at 17:41
  • You're starting a loop that never terminates itself, and running that loop on the UI thread. This is keeping the UI thread from processing UI related events. Commented Nov 27, 2013 at 17:43

3 Answers 3

2

Think event-driven... put a timer on your form. In the tick event do your work. Then just have the button toggle whether the timer is active...

    private void timer1_Tick(object sender, EventArgs e)
    {
        // Change your image here
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Toggle the timer's enabled state
        timer1.Enabled = !timer1.Enabled;
    }
Sign up to request clarification or add additional context in comments.

Comments

2

You probably want to use a Timer. Just drop one of those from your toolbox onto your form, and start/stop it when the button is pressed. Create a handler for the Tick event and perform the image swap within that.

3 Comments

How would I do this? Sorry I'm new to C#
I'd start by looking up Timer in MSDN. I'm pretty sure they have some code samples there.
Ehh.. follow his instructions on how to drag and drop a Timer onto your form?
0

There could be various ways:

  1. Implement Pause-Resume pattern using events signals (see this post for example)

  2. Keep a local bool variable (check it in each iteration - break if it is true, and update it once the button is pressed to true).

  3. Or timer (as suggested)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.