SpeechRecognitionEngine.LoadGrammar Method

Synchronously loads a specific grammar, as specified by a Grammar, for use by a SpeechRecognitionEngine.

Namespace: Microsoft.Speech.Recognition
Assembly: Microsoft.Speech (in microsoft.speech.dll)

Syntax

'Declaration

Parameters

  • grammar
    An instance of Grammar specify the grammar to add to an instance of SpeechRecognitionEngine.

Remarks

If the Grammar specified by the grammar parameter has already been loaded, a InvalidOperationException will be generated.

Example

The example below shows the atomic, synchronous loading and the unloading of instances of Grammar on a running SpeechRecognitionEngine by an anonymous method that handles RecognizerUpdateReached events, based on the user token custom object (GrammarRequest) supplied to the method.

recognizer.RecognizerUpdateReached +=
       delegate(object s, RecognizerUpdateReachedEventArgs args) {
           GrammarRequest request = args.UserToken as GrammarRequest; //cast and check if incoming type is a request
           if (request != null) { // We know this is a Grammar request now
               if (request.Grammar == null)
                   throw new ArgumentException("Invalid grammar used.");
           }
           switch (request.RequestType) {

               case GrammarRequestType.LoadGrammar:
                   RemoveDuplicateGrammar(request.Grammar);
                   _recognizer.LoadGrammar(request.Grammar);
                    break;
               case GrammarRequestType.UnloadGrammar:
                   _recognizer.UnloadGrammar(request.Grammar);
                   UpdateGrammarTree(_grammarTreeView, _recognizer);
                   //DisplayGrammarUnload(request.Grammar);
                   break;
           }

       };

}

Thread Safety

All public static (Shared in Visual Basic) members of this type are thread-safe. Instance members are not guaranteed to be thread-safe.

Platforms

Development Platforms

Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Vista Ultimate Edition, Windows Vista Business Edition, Windows Vista Enterprise Edition

Target Platforms

See Also

Reference

SpeechRecognitionEngine Class
SpeechRecognitionEngine Members
Microsoft.Speech.Recognition Namespace
RecognizerUpdateReached
UnloadAllGrammars
UnloadGrammar
SpeechRecognitionEngine.LoadGrammar Method