Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Make Program Wait For Button To Be Pressed

I want to make the program wait for a button to be pressed before it continues, I tried creating a while loop and having it loop until the button is clicked and sets a bool to true for the while loop to end, this made it crash

while (!Redpress)
{
    // I'd like the wait here
}

Redpress = false;

It doesn't matter whether or not the while is there, as long as the program waits for the button press before setting "Redpress" to false... Any Ideas?

like image 603
aelsheikh Avatar asked Apr 11 '11 22:04

aelsheikh


2 Answers

Use events - it's what they're designed for.

You don't need to use a boolean variable for this in the Button_Click event handler call your code:

private void Button_Click(object sender, EventArgs e)
{
    // The code you need to execute when the button is pressed
}

As @trickdev points out you will need to subscribe to this event but if you use the Events window in Visual Studio it will add the necessary code - including the empty handler - for you.

With event driven programs you are always waiting until the next "thing" happens. So in your case (if I've understood your application correctly) when you start the program it should simply tell the first button to flash "N" times. If you write that as event then the application will return to the waiting state once the code has completed.

Then in the button click event handler - you can subscribe all the buttons to the same event - you can check that the correct button has been pressed and then tell the next button to flash. If the wrong button was pressed then display a suitable message.

So in pseudo code you have:

public class Form
{
    Initialise()
    {
        this.Loaded += FormLoaded;
    }

    private void FormLoaded(object sender, EventArgs e)
    {
        // pick a button
        pickedButton.Flash();
    }

    private void Button_Click(object sender, EventArgs e)
    {
        if (sender == pickedButton)
        {
            pickedButton = pickButton();
        }
        else
        {
            message = "Sorry wrong button, try again";
        }

        pickedButton.Flash();
    }
}

public class Button
{
    public void Flash()
    {
        // loop N times turning button on/off
    }
}
like image 171
ChrisF Avatar answered Sep 18 '22 00:09

ChrisF


Windows Forms Controls have a Click event you can subscribe to in the form constructor:

myButton.Click += myButton_EventHandler;

You can then put whatever logic you want to happen in the handler and this will execute when the button has been clicked:

private void myButton_EventHandler(object sender, EventArgs e)
{
    Redpress = false;
}

You should avoid blocking (in any way, spinning sleeping etc) the Main thread in your forms applications as this will lock up the interface, there are many methods to avoid this including Timers, Threads, Delegates and BackgroundWorkers to name a few.

EDIT: To include your update

For this you could use a ManualResetEvent.

private readonly ManualResetEvent mre = new ManualResetEvent(false);

private void myButton_EventHandler(object sender, EventArgs e)
{
    mre.Set();
}

Your Form code can wait by calling:

mre.WaitOne();

This will make the executing code wait until the event has fired. Hope that helps.

NOTE: Please don't be mistaken though, unless you have some special case (I can't think of one off the top of my head at this time of night!) you should put the code straight in the event handler, rather than blocking a thread until the event has fired.

like image 39
trickdev Avatar answered Sep 20 '22 00:09

trickdev