Sound Command that can intercept the Thread RRS feed

  • Question

  • Good Day all 

    i am working on a Kinect Sensor Program, but i think this question can best be answered by the WPF forum. 

    In my Kinect Project , the User can use Kinect Gestures  or Voice Commands. THe Gestures work nicely its like one is using a mouse. i have a challenge with the Voice Commands which follow a certain Process. Please note that this functionality works well when one is not using voice commands.  

    Let me make a simple example. 

    i have a 3 Buttons 

    Button 1  = "System Ok " 

    Button 2  = " View Error" 

    Button 3  = "System Reboot" 

    Step 1: Now the logic say , if a user enter a wrong command , the System must play a sound for Error and the button for " View Error"  must Animate. 

    Step 2: The Rule goes on and say if the User has not click the " View Error"  within 5 seconds , Stop the Animation on the " View Error"

    Step 3:  and Animate the "System Reboot"  and play the Sound for "System Reboot" 

    Now on a normal button click with a Mouse , this would work. But now i am not clicking the Button Physically, i am using Voice Commands and i have trapped the SpeechRecognized event which is defined like this 

     private void SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
                // Speech utterance confidence below which we treat speech as if it hadn't been heard
                const double ConfidenceThreshold = 0.3;
                // Number of degrees in a right angle.
                const int DegreesInRightAngle = 90;
                // Number of pixels turtle should move forwards or backwards each time.
                const int DisplacementAmount = 60;
                if (e.Result.Confidence >= ConfidenceThreshold)
                    switch (e.Result.Semantics.Value.ToString())
                        case "VIEWERROR":
                            Error_Escaped = true;
                            btnViewError_Click(null, null); 
                        case "SYSTEMREBOOT":
                            btnSystemReboot_Click(null, null);
                        case "SYSTEMSTATUS":
                            SystemStatus_Click(null, null); 
                    // set the status text
                    this.Status.Content = Properties.Resources.GrammarConfidenceLow;
                    Logger.NormalLog("The Confidence is low , the Grammar is not Recognised as a Command", Generics.Generics.ActivePage) ;

    as you can see i am clicking the Buttons programmatically. So if i don't click the "View Error" button within 5 seconds , the System Reboot will start playing another sound.  

    That was about the Process. Now my challenge is that. After i used a wrong option according to my Process, the "View Error" button will give a sound and i will give a Voice Command to Stop the Sound , the next in the Process will still be fired. So i debugged the code and i realised that the Program , runs all the code that was running and when its done, it hit the "SpeechRecognized" event later, i want to intercept and give the Voice Command the same ability as a button click done with a mouse can do. 

    This is a function that runs , when there is an Error  

            private void ViewError()
                Error_Escaped = false;
                 if (Error_Escaped == false)

    This will set the Flag for "Error_Escape  = false " Which means no one has clicked the button. and it will Animate the Button and wait for 5 second which is my custom interval calculator. Now while its waiting  i would keep saying some Voice Command to Stop it. it will still continue while i wait , it will jump to System Reboot which will have its sound. i want the 

    SpeechRecognized(object sender, SpeechRecognizedEventArgs e)

    event to fire when i need it not after the other event of buttons are fired. 


    Vuyiswa Maseko

    Tuesday, November 15, 2016 11:02 PM

All replies

  • Hi  Vuyiswa Maseko,

    >>event to fire when i need it not after the other event of buttons are fired. 

    I suggest you can try to define a thread to run your Steps. The SpeechRecognized Sound Command that can intercept the Thread by the flag.

    The following code for your reference.

       public static bool flag = false;
            /// <summary>
            /// define a thread to run three steps.
            /// </summary>
            private void ThreadStart()
                Thread P_th = new Thread(() =>
                    int count = 1;
                    while (flag)
    (ThreadStart)(() =>
        if (count == 5)
            button1.Content = "System Ok ing......";
        else if (count == 10)
            button1.Content = "System Ok";
            button2.Content = "View Error ing....";
        else if (count >15)
            button1.Content = "System Ok";
            button2.Content = "View Error";
            button3.Content = " System Reboot ing .......";
            flag = false;
                P_th.IsBackground = true;

    Best Regards,
    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 16, 2016 2:07 PM
  • You can't just "intercept" the UI thread while it is running your ViewError method or some other code as your SpeechRecognized event handler won't even be executed until the UI thread is done processing the ViewError method. A single thread cannot execute two methods simultaneously.

    Instead of freezing the thread for 5 seconds you could use a Timer that calls the SystemReboot() method every 5 seconds unless the Error_Escaped flag has been set. Please refer to the following page on MSDN for more information about how to use the DispatcherTimer class in WPF: https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer%28v=vs.110%29.aspx.

    After you have created an instance of the DispatcherTimer and set it up and started it once, for example in the constructor of your class, you would then handle its Tick event something like this:

    private void dispatcherTimer_Tick(object sender, EventArgs e)
        if (Error_Escaped == false)

    The SpeechRecognized event handler can then be invoked while the timer is running and you can set the Error_Escaped flag to true and even reset the timer in there:

     private void SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
                 //handle event...

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread

    Thursday, November 17, 2016 6:48 PM