SpeechRecognitionEngine.UnloadGrammar Method
Unloads a grammar, as specified by an instance of Grammar, from an instance of SpeechRecognitionEngine.
Namespace: Microsoft.Speech.Recognition
Assembly: Microsoft.Speech (in microsoft.speech.dll)
Assembly: Microsoft.Speech (in microsoft.speech.dll)
If the Grammar specified by the grammar parameter is not currently loaded, TargetInvocationException 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 handling 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;
}
};
}