SpeechRecognitionEngine.LoadGrammarAsync Method
Asynchronously 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 TargetInvocationException will be generated.
On completion of the asynchronous loading of a Grammar, a LoadGrammarCompleted event is generated.
Example
The example below shows a general purpose method for adding Grammar objects synchronously and asynchronously, as well as adding a handler for LoadGrammarComplete events if needed.
private void AddGrammar(Grammar grammar, bool aSync) {
if (aSync == true) {
if (!_grammarRequestHandler) {
_recognizer.LoadGrammarCompleted += new EventHandler<LoadGrammarCompletedEventArgs>(onLoadGrammarCompleted);
_grammarRequestHandler = true;
}
_recognizer.LoadGrammarAsync(grammar);
} else if (!_recognizer.Enabled) {
_recognizer.LoadGrammar(grammar);
UpdateGrammarTree(_grammarTreeView, _recognizer); //needed as we are synchronous
} else {
_recognizer.RequestRecognizerUpdate(new GrammarRequest(grammar, GrammarRequestType.LoadGrammar));
}
}
//public delegate EventHandler onLoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs eventArgs);
public void onLoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs eventArgs) {
//Update Grammar display upon completion of asynchronous grammar load.
UpdateGrammarTree(_grammarTreeView, _recognizer);
}
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