【发布时间】:2021-12-12 18:23:15
【问题描述】:
我正在使用 .NET Framework 4.0 在 Visual Studios Community 2019 上开发一个小型 C++/CLR Windows 窗体项目,其中我有一个组合框和一个标签。
下面的代码片段可以正常工作:
private: System::Void comboBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
label1->Text = "comboBox1->Text";
}
但如果我在label1->Text = "comboBox1->Text"; 之后添加Sleep(1000);,我希望标签会在睡眠期之前更改,但在睡眠期结束后会更改。
一般来说,label1->Text = "comboBox1->Text"; 会在该行下方的任何内容之后执行。
对于下面的代码片段,我希望程序在更改 label1 文本后休眠。
private: System::Void comboBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
label1->Text = "comboBox1->Text";
Sleep(1000);
}
【问题讨论】:
-
按照设计,标签不会重新绘制自身,直到操作系统告诉它这样做。当 UI 线程处于睡眠状态时,这不会发生。 label1->Update() 在睡眠加速之前。永远不要让 UI 线程休眠,总是更喜欢 Timer。
标签: c++ visual-studio winforms visual-c++ clr