简体   繁体   中英

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?

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
    }
}

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.

If you need the program to wait for that event then don't write the waitForTheButton code in your main access/Page load .

Instead write your code inside the event. So you don't have to make an active wait. Events are for that.

eg: If you have something like this

static void Main()
{
    while (notPressed) { }
    DoThis();
    DoThat();
}

Then you could be changing it to:

static void Main
{
    myButton.OnClick += myButton_EventHandler;
}

private void myButton_EventHandler(object sender, EventArgs e)
{
    DoThis();
    DoThat();
}                                     

If you really want to work in a procedural paradigm even when you are using a OOP language, you should use a Semaphore or something like that. But that doesn't seem to be what you actually need , It's more like something you want .

Not sure why you want to stick there which I believe is the real problem. I guess you are in a code where in some cases you want to execute another code upon user action, then resume the remaining code in the main block.

When I deal with such cases, I use a Queue Controller class of my own. The class actually does AddAction(Action), PlayNext() and HasActions()

So at that point of yours I add the code after your loop to an action in my class and do AddAction.

I also do AddAction(Action, position) at the button event and then while HasActions() PlayNext()

This way I don't block the interface and make sure button code will run between my 2 pieces of code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM