C# Make Program Wait For Button To Be Pressed

后端 未结 3 748
有刺的猬
有刺的猬 2021-01-18 08:04

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

3条回答
  •  太阳男子
    2021-01-18 08:34

    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.

提交回复
热议问题