3

I have a void method (hopefully i am saying it correctly) that initializes a speech recognition engine, grammar and some paths for a text files to get the commands from. Now i have put the code inside a void method so that i can call it in the From_Load event but since for some reason the speech recognition does not work if the PC went to sleep and then back up, i setup a timer to call upon the method every ten min. Now the speech recog and grammar is re-initialized every ten min but im not sure if it is initialized twice or the first one is automatically terminated, if not, is it possible to do it?

  public void InitGrammar()
    {
        #region Recengine, grammar, commands
        try
        {

            SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();
            //Grammar DGrammar = new DictationGrammar();
            //recEngine.LoadGrammar(DGrammar);


            Choices commands = new Choices();
            Choices Ecommands = new Choices();
            Ecommands.Add(new string[] { "Emergency shutdown", "Reboot", "Abort", "Abort Abort" });
            commands.Add(File.ReadAllLines(@"C:\Natalie\commands\commands.txt"));
            GrammarBuilder gBuilder = new GrammarBuilder();
            gBuilder.Append(commands);
            Grammar grammar = new Grammar(gBuilder);
            synthesizer.SelectVoiceByHints(VoiceGender.Female);

            recEngine.LoadGrammarAsync(grammar);
            recEngine.SetInputToDefaultAudioDevice();

            recEngine.SpeechRecognized += RecEngine_SpeechRecognized;
            recEngine.RecognizeAsync(RecognizeMode.Multiple);


        }
        catch (Exception exinit)
        {
            _Errorsound.Play();
            MessageBox.Show(exinit.Message);

        }

        synthesizer.SelectVoiceByHints(VoiceGender.Female);

        #endregion
    }
NX_Glitch
  • 33
  • 4
  • 2
    You can write `return;` anywhere you want the method to end. – Rufus L Aug 14 '18 at 22:20
  • 2
    You should really add some hooks into the Windows API and reinitialize when the computer wakes up. Otherwise, you're playing a guessing game. See [Event to detect System wake up from sleep in C#](https://stackoverflow.com/q/18206183) – Heretic Monkey Aug 14 '18 at 22:22
  • Please try coding without ever doing `catch (Exception exinit)`. Catching the top-level exception is an anti-pattern. You should only ever catch **specific exceptions** that you can **meaningfully handle**. – Enigmativity Aug 15 '18 at 00:09
  • Heretic Monkey is exactly correct. You need to be event handling to catch when the program lifecycle-state changes. Any other approach is going to be an ugly hack. https://learn.microsoft.com/en-us/windows/uwp/launch-resume/app-lifecycle – Scuba Steve Aug 15 '18 at 00:29
  • 1
    An object variable can be checked for 'null'. If it's null, 'new' up an instance of it and do your 'init' code. If it's not null, then simply call 'return'. So, just make your engine a private global variable ... **private SpeechRecognitionEngine recEngine = null;** ... First line of code, in your init function - **if (recEngine != null) return;** – tobeypeters Aug 15 '18 at 02:51

1 Answers1

3

There's some confusion here due to wording. The original method call almost certainly finished in milliseconds, so the method is already "terminated" when your timer elapses. Nothing in your code indicates it is blocking (the event registration and "RecognizeAsync" in particular hints at this)

The side-effects of the method however may still (and certainly do in this case) remain. This is something you'll have to deal with by manually disposing/deregistering etc before re-running the initialization routine.

This is very important to understand from a conceptual perspective, but @Heretic Monkey makes an excellent point about your solution to this in general:

You should really add some hooks into the Windows API and reinitialize when the computer wakes up. Otherwise, you're playing a guessing game.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • I tried to add some hooks, without much success tho. (still a noob) so i tried reinitializing the speech recog, it works but then it answers twice when i give it commands. I will try the hooks again it does seem like the best option! – NX_Glitch Aug 14 '18 at 22:33
  • @NX_Glitch Until you deregister your old stuff correctly it won't matter if you are using a timer or the hooks. – BradleyDotNET Aug 14 '18 at 22:35