Search code examples
c#wpfkinectmessageboxgestures

Messagebox is displayed multiple times


I am using Kinect to develop an Image viewing application using C# and WPF. When the user puts both his hand above his head, I am displaying a Messagebox asking the user if he/she really wants to quit:

    //Quit if both hands above head
    if (skeleton.Joints[JointType.HandLeft].Position.Y > skeleton.Joints[JointType.Head].Position.Y &&
        skeleton.Joints[JointType.HandRight].Position.Y > skeleton.Joints[JointType.Head].Position.Y)
    {
        pause = true;
        quit();
    }

The quit() function is as follows:

    private void quit()
    {
        MessageBoxButton button = MessageBoxButton.YesNo;
        MessageBoxImage image = MessageBoxImage.Warning;
        MessageBoxResult result = MessageBox.Show("Are you sure you want to quit?", "Close V'Me :(", button, image);
        if (result == MessageBoxResult.Yes) window.Close();
        else if (result == MessageBoxResult.No)
        {
            pause = false;
        }

    }

The pause variable is used to stop recognizing any gestures while the MessageBox is still active. (pause = false means gesture recognition active, else no.)

So there are two problems:

  1. The MessageBox is displayed twice. If I click Yes in any one of those, the window closes. The behavior is okay but I dont want two windows.

  2. If I click No, another MessageBox gets displayed. If I click No on this box, another opens. This happens for every successive No that I click. After getting displayed 5-6 times approximately, it gives me the behavior I want, i.e, it changes the value of the pause variable to false and thus activates the recognition of other gestures.

What exactly am I doing wrong? What can I do to stop EVERYTHING and wait for the user to click on one of the options? Also why is the MessageBox being displayed multiple times (Twice initially, and more when I click No)?


Solution

  • It's hard to tell without looking at the code being debugged, but I'm guessing that your first block of code is being called in some sort of loop or event handler that is processing the latest Kinect data.

    Check for pause

    Try adding something like this above the first bit of code:

    if (pause)
       return;
    
    if (skeleton.Joints[JointType.HandLeft].Position.Y > skeleton.Joints[JointType.Head].Position.Y &&
        skeleton.Joints[JointType.HandRight].Position.Y > skeleton.Joints[JointType.Head].Position.Y)
    {
        pause = true;
        quit();
    }
    

    Timeout

    If this doesn't work, you might need to add a timeout period since the last time you prompt the user.

    private void quit()
    {
        if (DateTime.Now.Subtract(_lastQuitTime).TotalSeconds > 5)
        {
    
              //Prompt the user
    
              //Reset the timeout
              _lastQuitTime = DateTime.Now;
        }
    }