Search code examples
c#.netspeech-recognitiontext-to-speechspeech-synthesis

RaceOnRCWCleanup when running console app for speech recognition


I don't know either we can create a console app or not for speech recognition(searched for the same but didn't find any answer)and tried this code.

I have this code working in winforms app

but when trying to create this app in console visual studio is giving a very strange error mscorelib.pdb not found.And transferring to a page mscorelib.pdb

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

namespace ConsoleApplication2
{
    public  class Program
    {
        
        public static void Main(string[] args)
        {
            tester tst = new tester();
            tst.DoWorks();
           
        }

    }
    public class tester
    {
        SpeechSynthesizer ss = new SpeechSynthesizer();
        SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
        PromptBuilder pb = new PromptBuilder();
        Choices clist = new Choices();

        public void DoWorks()
        {
            clist.Add(new string[] { "how are you", "what is the current time", "open chrome", "hello" });

            Grammar gr = new Grammar(new GrammarBuilder(clist));

            sre.RequestRecognizerUpdate();
            sre.LoadGrammar(gr);
            sre.SetInputToDefaultAudioDevice();
            sre.SpeechRecognized += sre_recognised;
            sre.RecognizeAsync(RecognizeMode.Multiple);
        }

        public void sre_recognised(object sender, SpeechRecognizedEventArgs e)
        {
            switch (e.Result.Text.ToString())
            {
                case "hello":ss.SpeakAsync("Hello shekar");
                    break;
                case "how are you": ss.SpeakAsync("I am fine and what about you");
                    break;
                case "what is the time":ss.SpeakAsync("current time is: " + DateTime.Now.ToString());
                    break;
                case "open chrome":System.Diagnostics.Process.Start("chrome", "wwe.google.com");
                    break;
                default:  ss.SpeakAsync("thank you");
                    break;
            }
            Console.WriteLine(e.Result.Text.ToString());
        }
    }
}

here is the snapshot of the error page

enter image description here

I also loaded the given option "Microsoft.symbol.Server", but it is still giving the same output.

EDIT

HERE is the output window

enter image description here

Outputs are of big lengths ,so not being able to show all the outputs ,captured some relevant parts (regret).


Solution

  • You're seeing the debugger issuing a RaceOnRCWCleanup. The reason may be that you are instantiating but not properly cleaning up COM objects created under the hood by SpeechSynthesizer and/or SpeechRecognitionEngine.

    At the same time, a Console application is not automatically 'kept alive'. You need to specifically add code to prevent it from exiting immediately.

    You need to do 2 things:

    • Ensure your application stays alive long enough (for example, by adding a Console.ReadLine() statement in the Main method
    • Make sure that resources are properly cleaned up. Both SpeechRecognitionEngine and SpeechSynthesizer implement IDisposable, so they should be disposed when no longer needed. To do this properly, implement IDisposable in your tester class:

    Example:

     public class Tester 
     {
        SpeechSynthesizer ss = new SpeechSynthesizer();
        SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
        public void Dispose() 
        {
             ss.Dispose();
             sre.Dispose();
        }
     }