c++ - Visual Studio 2010 c++ sleep ()

标签 c++ winforms visual-studio-2010 sleep

我喜欢在 Windows 窗体项目中使用 Sleep() 函数,但 Sleep() 是在执行任何其他操作之前执行的。我读到我应该使用 fflush() 进行刷新,但我不知道要刷新什么。有人可以帮助我吗?

代码:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {                    

             this->label1->Visible= false;
             this->button1->Visible= false;


             r = (float)rand()/(float)RAND_MAX;                              
             r = r*100000;
             i = r;
             r = r - i;              

             String^ strR = "" + r;
             this->label2->Text = strR;


             if(r >= 0.5)
             {
                 this->pictureBox1->Visible= true;
                 this->pictureBox1->BackColor = System::Drawing::Color::Blue;                    
             }
             else
             {
                 this->pictureBox1->Visible= true;
                 this->pictureBox1->BackColor = System::Drawing::Color::Red;                                         
             }   

             Sleep(500);

         }

最佳答案

Sleep() 的调用阻塞了您的主 (UI) 线程,这会阻止消息泵更新您的控件。

在这种情况下,调用 Sleep 似乎并没有真正起到作用,除了阻止您的 UI - 如果您想防止再次按下按钮,更好的选择将禁用它,然后使用间隔为 500 毫秒的计时器 (System::Windows::Forms::Timer) 重新启用该按钮。

通过使用计时器,您不会阻塞 UI 线程,这允许您的控件保持事件状态,但您仍然阻止用户按下按钮。

关于c++ - Visual Studio 2010 c++ sleep (),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14129117/

相关文章:

c++ - 在调用删除之前删除动态分配的对象?

c++ - 在 Visual C++ 中 switch 是如何编译的,它的优化和速度如何?

c++ - 在鼠标悬停时更改图片框,并在鼠标离开时重置

winforms - WinForms 上的多项选择

visual-studio-2010 - Visual Studio 2010 : Visual Debugger Drill down into collection pain

C++/CLI 引发另一个对象的事件

visual-studio-2010 - 如何选择 Visual Studio 解决方案平台工具集以获得最大兼容性

c++ - C++中的函数重载与数字的数据类型

c++ - 是否可以重新启动 boost::future?

C#窗口窗体的透明背景