다음을 통해 공유


SpeechRecognitionEngine.RecognizeAsyncCancel 메서드

정의

현재 인식 작업이 완료되기를 기다리지 않고 비동기 인식을 종료합니다.

public:
 void RecognizeAsyncCancel();
public void RecognizeAsyncCancel();
member this.RecognizeAsyncCancel : unit -> unit
Public Sub RecognizeAsyncCancel ()

예제

다음 예제에서는 사용 방법을 보여 주는 콘솔 애플리케이션의 일부를 RecognizeAsyncCancel 메서드. 이 예제에서는 음성 인식 문법을 만들고 로드하고, 계속되는 비동기 인식 작업을 시작한 다음, 작업을 취소하기 전에 2초 동안 일시 중지합니다. 인식기가 c:\temp\audioinput\sample.wav 파일에서 입력을 받습니다. 이벤트 처리기는 작업 중에 인식기가 발생시키는 이벤트를 보여 주는 데 포함됩니다.

using System;
using System.Globalization;
using System.Speech.Recognition;
using System.Threading;

namespace AsynchronousRecognition
{
  class Program
  {
    // Indicate whether asynchronous recognition is complete.
    static bool completed;

    static void Main(string[] args)
    {
      // Create an in-process speech recognizer.
      using (SpeechRecognitionEngine recognizer =
        new SpeechRecognitionEngine(new CultureInfo("en-US")))
      {
        // Create and load a dictation grammar.
        Grammar dictation = new DictationGrammar();
        dictation.Name = "Dictation Grammar";

        recognizer.LoadGrammar(dictation);

        // Attach event handlers.
        recognizer.SpeechDetected +=
          new EventHandler<SpeechDetectedEventArgs>(
            SpeechDetectedHandler);
        recognizer.SpeechHypothesized +=
          new EventHandler<SpeechHypothesizedEventArgs>(
            SpeechHypothesizedHandler);
        recognizer.SpeechRecognitionRejected +=
          new EventHandler<SpeechRecognitionRejectedEventArgs>(
            SpeechRecognitionRejectedHandler);
        recognizer.SpeechRecognized +=
          new EventHandler<SpeechRecognizedEventArgs>(
            SpeechRecognizedHandler);
        recognizer.RecognizeCompleted +=
          new EventHandler<RecognizeCompletedEventArgs>(
            RecognizeCompletedHandler);

        // Begin asynchronous recognition from pre-recorded input.
        recognizer.SetInputToWaveFile(@"c:\temp\audioinput\sample.wav");

        completed = false;
        Console.WriteLine("Begin continuing asynchronous recognition...");
        recognizer.RecognizeAsync(RecognizeMode.Multiple);

        // Wait 2 seconds and then cancel the recognition operation.
        Thread.Sleep(TimeSpan.FromSeconds(2));
        recognizer.RecognizeAsyncCancel();

        // Wait for the operation to complete.
        while (!completed)
        {
          Thread.Sleep(333);
        }

        Console.WriteLine("Done.");
      }

      Console.WriteLine();
      Console.WriteLine("Press any key to exit...");
      Console.ReadKey();
    }

    // Handle the SpeechDetected event.
    static void SpeechDetectedHandler(object sender, SpeechDetectedEventArgs e)
    {
      Console.WriteLine(" In SpeechDetectedHandler:");
      Console.WriteLine(" - AudioPosition = {0}", e.AudioPosition);
    }

    // Handle the SpeechHypothesized event.
    static void SpeechHypothesizedHandler(
      object sender, SpeechHypothesizedEventArgs e)
    {
      Console.WriteLine(" In SpeechHypothesizedHandler:");

      string grammarName = "<not available>";
      string resultText = "<not available>";
      if (e.Result != null)
      {
        if (e.Result.Grammar != null)
        {
          grammarName = e.Result.Grammar.Name;
        }
        resultText = e.Result.Text;
      }

      Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",
        grammarName, resultText);
    }

    // Handle the SpeechRecognitionRejected event.
    static void SpeechRecognitionRejectedHandler(
      object sender, SpeechRecognitionRejectedEventArgs e)
    {
      Console.WriteLine(" In SpeechRecognitionRejectedHandler:");

      string grammarName = "<not available>";
      string resultText = "<not available>";
      if (e.Result != null)
      {
        if (e.Result.Grammar != null)
        {
          grammarName = e.Result.Grammar.Name;
        }
        resultText = e.Result.Text;
      }

      Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",
        grammarName, resultText);
    }

    // Handle the SpeechRecognized event.
    static void SpeechRecognizedHandler(
      object sender, SpeechRecognizedEventArgs e)
    {
      Console.WriteLine(" In SpeechRecognizedHandler.");

      string grammarName = "<not available>";
      string resultText = "<not available>";
      if (e.Result != null)
      {
        if (e.Result.Grammar != null)
        {
          grammarName = e.Result.Grammar.Name;
        }
        resultText = e.Result.Text;
      }

      Console.WriteLine(" - Grammar Name = {0}; Result Text = {1}",
        grammarName, resultText);
    }

    // Handle the RecognizeCompleted event.
    static void RecognizeCompletedHandler(
      object sender, RecognizeCompletedEventArgs e)
    {
      Console.WriteLine(" In RecognizeCompletedHandler.");

      if (e.Error != null)
      {
        Console.WriteLine(
          " - Error occurred during recognition: {0}", e.Error);
        return;
      }
      if (e.Cancelled)
      {
        Console.WriteLine(" - asynchronous operation canceled.");
      }
      if (e.InitialSilenceTimeout || e.BabbleTimeout)
      {
        Console.WriteLine(
          " - BabbleTimeout = {0}; InitialSilenceTimeout = {1}",
          e.BabbleTimeout, e.InitialSilenceTimeout);
        return;
      }
      if (e.InputStreamEnded)
      {
        Console.WriteLine(
          " - AudioPosition = {0}; InputStreamEnded = {1}",
          e.AudioPosition, e.InputStreamEnded);
      }
      if (e.Result != null)
      {
        Console.WriteLine(
          " - Grammar = {0}; Text = {1}; Confidence = {2}",
          e.Result.Grammar.Name, e.Result.Text, e.Result.Confidence);
      }
      else
      {
        Console.WriteLine(" - No result.");
      }

      completed = true;
    }
  }
}

설명

이 메서드는 비동기 인식을 즉시 마무리합니다. 현재 비동기 인식 작업이 입력을 수신하는 경우 입력이 잘리고 작업이 기존 입력으로 완료됩니다. 비동기 작업이 취소될 때 인식기는 또는 이벤트를 발생 RecognizeCompleted 시키고 의 trueRecognizeCompletedEventArgs 속성을 로 설정합니다Cancelled.EmulateRecognizeCompleted 이 메서드는 및 EmulateRecognizeAsync 메서드에 의해 RecognizeAsync 시작된 비동기 작업을 취소합니다.

입력을 잘리지 않고 비동기 인식을 중지하려면 메서드를 RecognizeAsyncStop 사용합니다.

적용 대상

추가 정보