SpeechRecognizer - The application called an interface that was marshalled for a different thread

0

I'm trying to make a simple speech controlled app, that is activated like cortana. When I speak something specific like "banana" I want the app to listen for commands. Here is my code:

        public async Task SetupRecognizer()
        {
            var WebGrammar = new SpeechRecognitionTopicConstraint(SpeechRecognitionScenario.WebSearch, "webSearch");
            Recognizer.Timeouts.InitialSilenceTimeout = TimeSpan.FromSeconds(6.0);
            Recognizer.Timeouts.BabbleTimeout = TimeSpan.FromSeconds(4.0);
            Recognizer.Timeouts.EndSilenceTimeout = TimeSpan.FromSeconds(1.2);

            Recognizer.ContinuousRecognitionSession.ResultGenerated += ContinuousRecognitionSession_ResultGenerated;

            await Recognizer.CompileConstraintsAsync();
        }

        public async Task Listen()
        {
            MessageDialog md = new MessageDialog("Now Listening", "Listening");
            await md.ShowAsync();

            await SetupRecognizer();

            SpeechRecognitionResult Result = await Recognizer.RecognizeAsync();

            await ExecuteCommand(Result.Text);

            if (ShouldShowSpeechRecognitionResult)
            {
                MessageDialog dialog = new MessageDialog(Result.Text, "Result");
                await dialog.ShowAsync();
            }

        }

        public async Task ListenFor()
        {
            ThisPage.ListenButton.IsEnabled = false;

            await SetupRecognizer();
            await Recognizer.ContinuousRecognitionSession.StartAsync(SpeechContinuousRecognitionMode.Default);
        }

        private async void ContinuousRecognitionSession_ResultGenerated(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionResultGeneratedEventArgs args)
        {
            if (args.Result.Text.Contains("banana"))
            {
                await Recognizer.ContinuousRecognitionSession.CancelAsync();
                await Listen();
            }
        }

(The ListenFor() task is called by a button click event.)

When I run the app and say "banana" it does call the Recognizer.ContinuousRecognitionSession.CancelAsync() function, but when it gets to 'Listen()' Visual Studio throws an exception:

'The application called an interface that was marshalled for a different thread.
(Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))'
c#
exception
speech-recognition
asked on Stack Overflow Dec 12, 2017 by J0K3R_12QQ

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0