title: Speech recognition using SetInputToWaveFile ends prematurely
body: <p>I want to do speech recognition of an audio file.</p>
  
  <p>My code is pretty basic and derived from <a href="http://msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognitionengine.setinputtowavefile.aspx" rel="nofollow">here</a>. The problem is that it stops with every wave file prematurely after a few seconds even though some wave files are hours long.</p>
  
  <p>How to make it scan the whole file?</p>
  
  <pre><code>namespace Stimmenerkennung
  {
      public partial class Form1 : Form
      {
          //...
          Thread erkennung;
          bool completed;
  
          private void Form1_Load(object sender, EventArgs e)
          {
              erkennung = new Thread(erkennen);
              erkennung.Start();
          }
  
          void erkennen()
          {
              using (SpeechRecognitionEngine recognizer =
                 new SpeechRecognitionEngine())
              {
  
                  // Create and load a grammar.
                  Grammar dictation = new DictationGrammar();
                  dictation.Name = "Dictation Grammar";
  
                  recognizer.LoadGrammar(dictation);
  
                  // Configure the input to the recognizer.
                  recognizer.SetInputToWaveFile(@"REC01.wav");
  
  
                  // Attach event handlers for the results of recognition.
                  recognizer.SpeechRecognized +=
                    new EventHandler&lt;SpeechRecognizedEventArgs&gt;(recognizer_SpeechRecognized);
                  recognizer.RecognizeCompleted +=
                    new EventHandler&lt;RecognizeCompletedEventArgs&gt;(recognizer_RecognizeCompleted);
  
                  // Perform recognition on the entire file.
                  db("Starting asynchronous recognition...");
                  recognizer.RecognizeAsync();
                  while (!completed)
                  {
                      //fs((int)(100 / recognizer.AudioPosition.TotalSeconds * recognizer.AudioPosition.Seconds));
                      db(recognizer.AudioState.ToString());
                      Thread.Sleep(100);
                  }
              }
          }
  
          // Handle the SpeechRecognized event.
          void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
          {
              if (e.Result != null &amp;&amp; e.Result.Text != null)
              {
                  db(e.Result.Text);
              }
              else
              {
                  db("  Recognized text not available.");
              }
          }
  
          // Handle the RecognizeCompleted event.
          void recognizer_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
          {
              if (e.Cancelled)
              {
                  db("  Operation cancelled.");
              }
              if (e.InputStreamEnded)
              {
                  db("  End of stream encountered.");
              }
              completed = true;
          }
  
          void db(string t)
          {
              this.textBox1.Invoke((MethodInvoker)delegate
              {
                  textBox1.Text = textBox1.Text + Environment.NewLine + t;
                  //textBox1.Text = t;
              });
          }
      }
  }
  </code></pre>