简体   繁体   中英

Using timers in c# with Kinect features

I am trying to use a timer to produce an error box after a certain amount of time.

I am currently using Kinect and the face properties.

This is what I have so far:

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;
}

I don't think it's right because as soon as I look away, the message box just keeps spamming the system.

This is what I am really after:

As soon as the person looks away, I want a timer to start so that if they look away for more than 10 seconds, the message box appears on the screen, just the one. And you have to select "OK" for the system to keep running again. Anything under 10 seconds then the system ignores this.

Am I on the right lines with this code please?

A simple way to do this is to use 2 timers. One timer will start when a person looks away. The other timer will poll ever 50ms to check if the person is looking.

    //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;
            }

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM