SpeechRecognitionEngine.RecognizeAsyncCancel Method

Terminates asynchronous recognition without waiting for the current recognition operation to complete.

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

public void RecognizeAsyncCancel()

This method immediately finalizes asynchronous recognition. If the current asynchronous recognition operation is receiving input, the input is truncated and the operation completes with the existing input. The recognizer raises the RecognizeCompleted or EmulateRecognizeCompleted event when an asynchronous operation is canceled, and sets the Cancelled property of the RecognizeCompletedEventArgs to true. This method cancels asynchronous operations initiated by the RecognizeAsync and EmulateRecognizeAsync methods.

To stop asynchronous recognition without truncating the input, use the RecognizeAsyncStop() method.

The following example shows a graphical user interface which uses a button to start and stop a SpeechRecognitionEngine. When the user clicks the start/stop button while the SpeechRecognitionEngineis running, either RecognizeAsyncCancel or RecognizeAsyncStop is called depending on the state of the application.

private void _startRecogButton_Click(object sender, EventArgs eventArgs) {
    if (_startRecogButton.Text == "Start Speech Recognition") { //Toggle Speech Recognition on
        _startRecogButton.Text = "Stop Speech Recognition";
        if (_useMultiple) {
            _recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
        } else {
            _recognitionEngine.RecognizeAsync(RecognizeMode.Single);
        }
    } else {                                                      //Toggle Speech Recognition off
        _startRecogButton.Text = "Start Speech Recognition";
        if (_friendlyStop) {
            _recognitionEngine.RecognizeAsyncStop(); //Stop after current phrase is finished.
        } else {
            _recognitionEngine.RecognizeAsyncCancel(); //Stop before current phrase is finished.
        }

    }
}
Show: