[英]How do I stop an Application DoEvents loop in C++?
This is a Visual Studio Express C++ Windows Forms application. 这是一个Visual Studio Express C ++ Windows窗体应用程序。
I want to play a WAV file strFileName.WAV in a WAV play wavPlayer every 10 seconds from the time I press the "Start" button until such time as I press the "Stop" button. 我想在按下“开始”按钮之前每隔10秒在WAV播放wavPlayer中播放一个WAV文件strFileName.WAV,直到我按下“停止”按钮。
When each 10 second interval is up, TimerEventProcessor plays the WAV file. 当每个10秒间隔结束时,TimerEventProcessor将播放WAV文件。
The problem is that I have to press "Stop" twice to get it to work. 问题是我必须按两次“停止”才能使它工作。 The first press of "Stop" seems to be ignored.
第一次按“停止”似乎被忽略了。
Why is btnStop_Click not executing the first time I press "Stop"? 为什么第一次按“停止”时btnStop_Click没有执行?
private: System::Void bntStart_Click(System::Object^ sender, System::EventArgs^ e) {
if (String::IsNullOrEmpty(strFileName)) {
lblRunning->Text = L"Cannot Start Until File Is Loaded";
}
else {
lblRunning->Text = L"Running";
myTimer->Interval = iIntervalSeconds * 1000;
myTimer->Tick += gcnew EventHandler( TimerEventProcessor );
myTimer->Enabled = true;
while (lblRunning->Text == L"Running") {
Application::DoEvents();
}
}
}
private: System::Void btnStop_Click(System::Object^ sender, System::EventArgs^ e) {
lblRunning->Text = L"Stopped";
myTimer->Enabled = false;
wavPlayer->Stop();
}
Get rid of the 摆脱了
while (lblRunning->Text == L"Running") {
Application::DoEvents();
}
loop. 环。 When you return from
bntStart_Click
the form will return to dispatching messages and the timer will tick as expected. 当您从
bntStart_Click
返回时,表单将返回调度消息,计时器将按预期打勾。 There is no need for you to create a manual Application::DoEvents()
loop, which is probably the cause of your problem. 您无需创建手动
Application::DoEvents()
循环,这可能是您的问题的原因。
By calling Application::DoEvents();
通过调用
Application::DoEvents();
in a loop you are creating a pooling loop for window messages. 在循环中,您正在为窗口消息创建池化循环。 Without any sleep call this loop cause 100% CPU usage.
没有任何睡眠调用,此循环会导致100%的CPU使用率。 By letting your
bntStart_Click
return the WinForms runtime will sleep your process until a message is ready - resulting in very low CPU usage. 通过让你的
bntStart_Click
返回WinForms运行时将睡眠你的进程,直到消息准备好 - 导致CPU使用率非常低。
This high CPU usage is probably making your application unresponsive which is why it seems that clicks are not being processed. 这种高CPU使用率可能会使您的应用程序无响应,这就是为什么似乎没有处理点击的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.