【发布时间】:2016-07-21 02:51:16
【问题描述】:
我对下面的代码有一些问题。
当应用程序运行时,只要LookingAwayResult.Text = "Yes",计时器就会启动并计数到 10。当 LookingAwayResult.Text = "No" 或 "Maybe" 时,计时器应该停止并再次重置回 0,但这不会。
当计时器达到 10 时,会出现一个消息框,这是我想要的,但这会继续显示并在我的屏幕上显示垃圾邮件。计时器用于在消息框出现后重置为 0,并且应用程序冻结,直到在消息框上选择“Ok”。
似乎我的代码正在循环所有不是我想要的计时器。
private void OnFaceFrameArrived(object sender, FaceFrameArrivedEventArgs e)
{
// Retrieve the face reference
FaceFrameReference faceRef = e.FrameReference;
if (faceRef == null) return;
// Acquire the face frame
using (FaceFrame faceFrame = faceRef.AcquireFrame())
{
if (faceFrame == null) return;
// Retrieve the face frame result
FaceFrameResult frameResult = faceFrame.FaceFrameResult;
// Display the values
HappyResult.Text = frameResult.FaceProperties[FaceProperty.Happy].ToString();
EngagedResult.Text = frameResult.FaceProperties[FaceProperty.Engaged].ToString();
GlassesResult.Text = frameResult.FaceProperties[FaceProperty.WearingGlasses].ToString();
LeftEyeResult.Text = frameResult.FaceProperties[FaceProperty.LeftEyeClosed].ToString();
RightEyeResult.Text = frameResult.FaceProperties[FaceProperty.RightEyeClosed].ToString();
MouthOpenResult.Text = frameResult.FaceProperties[FaceProperty.MouthOpen].ToString();
MouthMovedResult.Text = frameResult.FaceProperties[FaceProperty.MouthMoved].ToString();
//initilize look away timer for 10 seconds
Timer lookAwayTimer = new Timer(interval: 10000);
//inialize the poll tiomer for 50 ms
Timer pollTimer = new Timer(interval: 50);
LookingAwayResult.Text = frameResult.FaceProperties[FaceProperty.LookingAway].ToString();
//if 10 seconds expires then show message box
lookAwayTimer.Elapsed += (s, f) =>
{
MessageBox.Show("Looking is set to yes", "Looking Error", MessageBoxButton.OK);
};
//enable poll timer
pollTimer.Enabled = true;
//check if person is looking. If they are not then enable the lookAwayTimer. If they start looking
//then disable the timer
pollTimer.Elapsed += (s, f) =>
{
Check = frameResult.FaceProperties[FaceProperty.LookingAway].ToString();
if (Check == "Yes")
{
lookAwayTimer.Enabled = true;
}
else
{
lookAwayTimer.Enabled = false;
}
};
}
}
我所追求的是在该人不看并停止后运行计时器,并在该人再次看时重置回0。
当计时器达到 10 秒时,会出现消息框并且应用程序会冻结。用户必须选择“确定”才能使此框消失并且应用程序重置为默认值。
根据研究,我相信在这里使用全局变量或模态框可能会派上用场?
我相信使用模态框会冻结我的应用程序,直到用户对其进行操作?但这仍然不能解决我的问题,即计时器没有重置回 0,并且希望应用程序在选择“确定”后完全重置。
我也很喜欢 C# 中的全局变量,除非必要,否则应该避免使用。
如果模态框是其中一部分的答案,我是否只需将MessageBox.Show 更改为ShowDialog?
【问题讨论】:
标签: c# timer global-variables