简体   繁体   English

运行用于语音识别的控制台应用程序时的RaceOnRCWCleanup

[英]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 我有此代码在winforms应用程序中工作

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 但是当尝试在控制台Visual Studio中创建此应用程序时,发现了一个非常奇怪的错误mscorelib.pdb,并转移到了页面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 这是错误页面的快照

在此处输入图片说明

I also loaded the given option "Microsoft.symbol.Server", but it is still giving the same output. 我还加载了给定的选项“ Microsoft.symbol.Server”,但它仍提供相同的输出。

EDIT 编辑

HERE is the output window 这里是输出窗口

在此处输入图片说明

Outputs are of big lengths ,so not being able to show all the outputs ,captured some relevant parts (regret). 输出的长度很大,因此无法显示所有输出,因此捕获了一些相关部分(遗憾)。

You're seeing the debugger issuing a RaceOnRCWCleanup . 您正在看到调试器发出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 . 其原因可能是您正在实例化,但没有正确清理由引擎盖下创建COM对象SpeechSynthesizer和/或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 确保您的应用程序存活时间足够长(例如,通过在Main方法中添加Console.ReadLine()语句Console.ReadLine()
  • Make sure that resources are properly cleaned up. 确保正确清理资源。 Both SpeechRecognitionEngine and SpeechSynthesizer implement IDisposable , so they should be disposed when no longer needed. SpeechRecognitionEngine和SpeechSynthesizer都实现IDisposable ,因此应在不再需要它们时将它们丢弃。 To do this properly, implement IDisposable in your tester class: 要正确执行此操作,请在您的测试人员类中实现IDisposable

Example: 例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM