简体   繁体   English

条件为假时执行 IF 语句 C#

[英]IF statement executes when condition is false C#

  • I tried disabling the button at first but that didn't work我一开始尝试禁用按钮,但没有奏效
  • Therefore I created a variable to prevent it from running the code inside the IF.因此我创建了一个变量来防止它在 IF 中运行代码。 For some weird reason it doesn't work, when I try to click it before the sleep it executes right after the time is over.由于某些奇怪的原因它不起作用,当我尝试在睡眠之前单击它时,它会在时间结束后立即执行。

So if I click the Button所以如果我点击按钮

  • one time it sleeps for 2 seconds,有一次它睡了 2 秒,
  • if I double click it it sleeps for 4 seconds (etc) despite that it should only sleep for 2 seconds.如果我双击它,它会休眠 4 秒(等等),尽管它应该只休眠 2 秒。

This should make so the code would only executes after the buttons are enabled again.这应该使代码仅在再次启用按钮后才会执行。 (The buttons being disabled is just for the looks) (被禁用的按钮只是为了外观)

Note:Variable "button_enabled" is global注意:变量“button_enabled”是全局变量

 private void senha_b_Click(object sender, EventArgs e)
    {
        if (button_enabled == 1)
        {
            button_enabled = 0;
            wait_label.Show();
            senha_a.Enabled = false;
            senha_b.Enabled = false;
            senha_c.Enabled = false;

            System.Threading.Thread.Sleep(2000);

            wait_label.Hide();
            senha_a.Enabled = true;
            senha_b.Enabled = true;
            senha_c.Enabled = true;
            button_enabled = 1;
        }
    }

This should fix it这应该解决它

 private async void senha_b_Click(object sender, EventArgs e)
    {
        if (button_enabled == 1)
        {
            button_enabled = 0;
            wait_label.Show();
            senha_a.Enabled = false;
            senha_b.Enabled = false;
            senha_c.Enabled = false;

            await Task.Delay(2000);

            wait_label.Hide();
            senha_a.Enabled = true;
            senha_b.Enabled = true;
            senha_c.Enabled = true;
            button_enabled = 1;
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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