This topic has not yet been rated - Rate this topic

SpeechRecognitionEngine.LoadGrammar Method

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

Namespace:  System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)
public void LoadGrammar(
	Grammar grammar
)

Parameters

grammar
Type: System.Speech.Recognition.Grammar
An instance of Grammar specify the grammar to add to and instance of SpeechRecognitionEngine.

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

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;
           }

       };

}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ