1

I have a voice activated program that is throwing the only error. Its a System.InvalidOperationException I have tried numerous methods and I can not figure out why it is not being corrected.

I am using System.Speech. I have a timer and in the timer, i have it enabled and disabled it from True to False and False to True. Neither of it fixes the issue.

I have declared my Class

SpeechRecognitionEngine startlistening = new SpeechRecognitionEngine();

I have specified my Events

startlistening.SetInputToDefaultAudioDevice();
startlistening.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices("alexis"))));
startlistening.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(startlistening_SpeechRecognized);

and then I have placed the command

case "quit listening":      
Alexis.SpeakAsync("I will await further commands ");
_recognizer.RecognizeAsyncCancel();
startlistening.RecognizeAsync(RecognizeMode.Multiple);
break;

then i have placed my timer

private void tmrSpeech_Tick(object sender, EventArgs e)
    {
        if (recTimeOut == 10)
        {
            _recognizer.RecognizeAsyncCancel();
        }
        else if (recTimeOut == 11)
        {
            startlistening.RecognizeAsync(RecognizeMode.Multiple);
            tmrSpeech.Stop();
            recTimeOut = 0;
        }
        recTimeOut += 1;
    }

I have declared the struct for Start listening

void startlistening_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {

        string speech = e.Result.Text;
        switch (speech)
        {
            case "alexis":
                startlistening.RecognizeAsyncCancel();
                Alexis.SpeakAsync("I am back online");
                _recognizer.RecognizeAsync(RecognizeMode.Multiple);
                break;
        }
    }

I am at a loss. I have all references set, as well as the timer set on

Enabled False
Interval 10000
GenerateMember True
Modifiers Private

Could there be something i am missing. When i tell the program to Quit Listening it uses the AsyncCancel and then if say Alexis then the program responds with no errors. However if i say Quit Listening and wait longer than 10 seconds it will throw the Exception. I have tried everything i know to do. Any Ideas? I am using Visual Studio 2013 Community and its in Winforms and .NET 4.5 Framework

Saagar Elias Jacky
  • 2,684
  • 2
  • 14
  • 28
Ian Massey
  • 25
  • 3
  • 5
    What does the exception say? – SLaks Apr 19 '15 at 23:24
  • Include the full text of the exception, the message will contain important details – Scott Chamberlain Apr 19 '15 at 23:42
  • And which line of code is causing the exception... – Rufus L Apr 20 '15 at 00:41
  • It occurs in the tmrSpeech_Tick – Ian Massey Apr 20 '15 at 01:26
  • An unhandled exception of type 'System.InvalidOperationException' occurred in System.Speech.dll Additional information: Cannot perform this operation while the recognizer is doing recognition. – Ian Massey Apr 20 '15 at 01:26
  • {"Cannot perform this operation while the recognizer is doing recognition."} – Ian Massey Apr 20 '15 at 01:27
  • Please post the code for `RecognizeAsyncCancel();`... Apparently this part is not canceling the Recognition process – Saagar Elias Jacky Apr 20 '15 at 02:34
  • The fact that your example is full of DoSomethingAsync() calls and not one of them is actually awaited is concerning. If those methods are really returning Task or Task, you might have some luck awaiting them. If they're old style APM methods try creating tasks from them with the FromAsync helpers (https://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.fromasync(v=vs.110).aspx) – ScheuNZ Apr 20 '15 at 03:29
  • @ScheuNZ well its a speech program. When Alexis is just sitting idle and when i am talking it will try to execute commands. So the Quit talking Cancels everything and mutes the mic. Then i just say Alexis then she come back online. However the mic is not fully muted. Its like when you use the standard Microsoft Speech Recognition. You start listening then you can execute commands. Then you can say stop listening and it turns off the little microphone. This is the same concept. But when i say quit listening after about 10 - 20 seconds it throws the error – Ian Massey Apr 20 '15 at 03:53
  • @SheuNZ the link you posted really is for executing tasks. This for the stop listening. I know about the link you posted because I have over 1500 lines of code using SpeakAsync. However the program works great. Its this error and the word Arkansas. But the word Arkansas you have to pronounce R-Kansas. But that is another tweak i have to figure out. So in the long run I can reprogram it to just say Mute but then i have to invoke a VK to mute but then when its mute it cuts all sound not just the program – Ian Massey Apr 20 '15 at 03:59
  • @Ian Massey, okay I understand. My point was that the error suggests you're performing an operation when one of the speech related instances isn't in an appropriate state. The articles http://stackoverflow.com/questions/17937305/speech-recognition-engine-does-not-shut-down-invalid-operation-exception and http://stackoverflow.com/questions/18774802/system-speech-recognition-error both describe cases where this can occur. As per the earlier comments, the full call stack (with 'Just My Code' disabled) might enable someone here to help, assuming the linked articles don't pan out. – ScheuNZ Apr 20 '15 at 07:55
  • @ScheuNZ I have read both of the articles. However they did not help. It sparked a question though. I have one timer that is enabled. It auto checks to every 30 minutes to make sure my command list is updated. Now is it possible the timer is causing the error? However the timer does not use the Speech unless you say Update Commands and this overrides the timer. I you don't say Update commands then the timer is active. Do you think this could be it. – Ian Massey Apr 20 '15 at 16:19
  • @Ian Massey, it's hard to say without a call stack showing the source of the error, or a small repro. If you're reproducing the problem more than once every thirty minutes, it seems unlikely your other timer is responsible but if you've ruled everything else out it can't hurt to investigate that. – ScheuNZ Apr 20 '15 at 18:27
  • @ScheuNZ Update I have turned all my timers off. The problems still persists. The only command i have said was to quit listening. Then a few minutes later it still throws the error. I am at a loss on this. I don't know where to go from here. The recognizer is not doing anything but it says it can't quit listening. It throws the error on the timer where it says startlistening.RecognizeAsync(RecognizeMode.Multiple); on the else if statement of the timer – Ian Massey Apr 24 '15 at 15:12

1 Answers1

1

Yes Its too late but for others it may help .

In SpeechRecognized event SpeechRecogniser have not completed its action. But in your code you are forcing SpeechRecogniser to start listening. This action is not possible by SpeechRecogniser.

startlistening.RecognizeAsyncCancel();
 Alexis.SpeakAsync("I am back online");
 _recognizer.RecognizeAsync(RecognizeMode.Multiple);

You should start listening after SpeechRecognized event is completed its execution.

You can use to RecognizeCompleted event for, _recognizer.RecognizeAsync(RecognizeMode.Multiple);

S Prashant
  • 26
  • 1