【问题标题】:Using timers in c# with Kinect features在 c# 中使用带有 Kinect 功能的计时器
【发布时间】:2016-03-28 17:38:23
【问题描述】:

我正在尝试使用计时器在一定时间后产生一个错误框。

我目前正在使用Kinect 和人脸属性。

这是我目前所拥有的:

LookingAwayResult.Text = frameResult.FaceProperties[FaceProperty.LookingAway].ToString();

Check = frameResult.FaceProperties[FaceProperty.LookingAway].ToString();

int TimeDelay = 5000;
if (Check == "Yes")
{
    Thread.Sleep(TimeDelay);

    MessageBox.Show("Looking is set to Yes", "Looking Error",
        MessageBoxButton.OK, MessageBoxImage.Exclamation
    );
    LookingAwayResult.Text = Check;
}

我认为这不对,因为我一移开视线,消息框就会不断向系统发送垃圾邮件。

这才是我真正追求的:

当这个人移开视线时,我想要一个计时器启动,这样如果他们移开视线超过 10 秒,屏幕上就会出现消息框,只有一个。您必须选择“确定”,系统才能继续运行。任何低于 10 秒的内容都会被系统忽略。

请问我的代码是否正确?

【问题讨论】:

    标签: c# timer


    【解决方案1】:

    一个简单的方法是使用 2 个计时器。当一个人移开视线时,将启动一个计时器。另一个计时器将轮询 50ms 以检查此人是否正在查看。

        //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);
        public ClassConstructor()
        {
            //if 10 seconds expires then show message box
            lookAwayTimer.Elapsed += (s, e) =>
            {
                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 then enable the lookAwayTimer.  If they stop looking
            //then disable the timer
            pollTimer.Elapsed+=(s,e)=>
            {
                LookingAwayResult.Text = frameResult.FaceProperties[FaceProperty.LookingAway].ToString();
    
                Check = frameResult.FaceProperties[FaceProperty.LookingAway].ToString();
    
                if(Check=="Yes")
                {
                    lookAwayTimer.Enabled = true;
                }
                else
                {
                    lookAwayTimer.Enabled = false;
                }
    
            }
        }
    

    【讨论】:

    • OP 是计时器的新手,您的示例使用 LINQ?
    猜你喜欢
    • 2019-08-04
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 2021-01-20
    相关资源
    最近更新 更多