none
Error "The Thread does not exist in the current context" RRS feed

  • Question

  • I am having the error "The Thread does not exist in the current context".Can some one please help me?

    The code is as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Speech;
    using System.Speech.Recognition;

    namespace ConsoleApplication1
    {
        class Program
        {
            static bool completed;
            static void Main(string[] args)
            {
                using (SpeechRecognizer recognizer = new SpeechRecognizer())
          {
             
            // Create and load a sample grammar.
            Grammar testGrammar =
              new Grammar(new GrammarBuilder("testing testing"));
            testGrammar.Name = "Test Grammar";
            recognizer.LoadGrammar(testGrammar);

            // Attach event handlers for recognition events.
            recognizer.SpeechRecognized +=
              new EventHandler<SpeechRecognizedEventArgs>(
                SpeechRecognizedHandler);
            recognizer.EmulateRecognizeCompleted +=
              new EventHandler<EmulateRecognizeCompletedEventArgs>(
                EmulateRecognizeCompletedHandler);

            completed = false;

            // Start asynchronous emulated recognition. 
            // This matches the grammar and generates a SpeechRecognized event.
            recognizer.EmulateRecognizeAsync("testing testing");

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

            completed = false;

            // Start asynchronous emulated recognition.
            // This does not match the grammar or generate a SpeechRecognized event.
            recognizer.EmulateRecognizeAsync("testing one two three");

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

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

        // Handle the SpeechRecognized event.
        static void SpeechRecognizedHandler(
          object sender, SpeechRecognizedEventArgs e)
        {
          if (e.Result != null)
          {
            Console.WriteLine("Recognition result = {0}",
              e.Result.Text ?? "<no text>");
          }
          else
          {
            Console.WriteLine("No recognition result");
          }
        }

        // Handle the SpeechRecognizeCompleted event.
        static void EmulateRecognizeCompletedHandler(
          object sender, EmulateRecognizeCompletedEventArgs e)
        {
          if (e.Result == null)
          {
            Console.WriteLine("No result generated.");
          }

          // Indicate the asynchronous operation is complete.
          completed = true;
        }
      }

            }

    Tuesday, February 13, 2018 3:17 PM

All replies

  • Try adding ‘using System.Threading’ or type the full name: ‘System.Threading.Thread.Sleep(333)’.

    Tuesday, February 13, 2018 5:57 PM
  • I doubt that that code compiles. The Main method consists of just one line, a using statement without a block.  So we can't trust the code to be relevant.

    You don't say where the error is happening.

    The Thread.Sleep documentation says:

    However, we recommend that you use other System.Threading classes such as Mutex, Monitor, EventWaitHandle, or Semaphore instead to sychronize threads or manage resources.

    And that sure applies here. Microsoft did not design asynchronous methods like EmulateRecognizeAsync to be synchronized using Sleep. One problem is that Sleep is imprecise; it can end too early or it can add an unnecessary amount of time.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, February 13, 2018 6:59 PM