title: Loading a new grammar in Microsoft.Speech.Recognition
body: <p>I am building a program that uses the new microsoft.speech API. When I first start the program I load my SpeechRecognizer with a grammar and everything is fine. However, I also am using the Microsoft Async CTP to do some work to retrieve a word to be added to the grammar and when it returns I want to load a new grammar. Bellow is the Async CTP portion...</p>
  
  <pre><code>class UR
  {
      private CancellationTokenSource _cancelationToken;
  
      public UR()
      {
          _cancelationToken = new CancellationTokenSource();
      }
  
      public async void StartRecognition()
      {
          String someword = await Recognize(_cancelationToken.Token);
          //Load the new grammar
          SpeechRecognizer sr = SpeechRecognizer.Instance;
          sr.LoadNewGrammar(someword);
      }
  }
  </code></pre>
  
  <p>Here is the LoadNewGrammar method, in order to load the new grammar you will see that I am trying to use "RecognizeAsyncCancel" to stop the recognizer, then load the grammar and user "RecognizeAsync" to restart the recognizer.</p>
  
  <pre><code>    public void LoadNewGrammar(String someword)
      {
          Commands = new Dictionary&lt;string, WhatSaid&gt;()
          {
              {"DoThis",         new WhatSaid()      {verb=Verbs.DoThis}},
              {"DoThat",         new WhatSaid()      {verb=Verbs.DoThat}},
              {someword,         new WhatSaid()      {verb=Verbs.Someword}}
          };
  
          if (sre == null)
          {
              RecognizerInfo ri = GetKinectRecognizer();
              sre = new SpeechRecognitionEngine(ri);
          }
          //Stop the speech recognizer temporarily in order to load the new grammar
          sre.RecognizeAsyncCancel();
  
          // Build a grammar of commands
          var basicCmds = new Choices();
          foreach (var phrase in Commands)
          {
              basicCmds.Add(phrase.Key);
          }
  
          // Combine all choices 
          var allChoices = new Choices();
          allChoices.Add(basicCmds);
  
          //Create a grammar builder to be used in the grammar object
          var gb = new GrammarBuilder();
          gb.Culture = sre.RecognizerInfo.Culture; 
          gb.Append(allChoices);
  
          var g = new Grammar(gb);
          sre.LoadGrammar(g);
          sre.RecognizeAsync(RecognizeMode.Multiple);
      }
  </code></pre>
  
  <p>The problem seems to be that when I call sre.RecognizeAsyncCancel my program completely freezes up. I have also tried RecognizeAsyncStop with the same result. I have a feeling that because I am having a threading issue here but have no idea where to begin to fix this.     </p>