【问题标题】:RaceOnRCWCleanup when running console app for speech recognition运行控制台应用程序进行语音识别时的 RaceOnRCWCleanup
【发布时间】:2018-08-13 14:18:25
【问题描述】:

我不知道我们是否可以创建控制台应用程序用于语音识别(搜索相同但没有找到任何答案)并尝试了此代码。

我有这段代码在 winforms 应用程序中工作

但是当试图在控制台 Visual Studio 中创建这个应用程序时,会给出一个非常奇怪的错误 mscorelib.pdb not found.And transfer 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());
        }
    }
}

这是错误页面的快照

我还加载了给定选项“Microsoft.symbol.Server”,但它仍然给出相同的输出。

编辑

这里是输出窗口

输出很长,所以无法显示所有输出,捕获了一些相关部分(遗憾)。

【问题讨论】:

  • 这不是真正的错误。这只是一个警告说找不到调试数据。实际的例外情况有所不同。查看输出窗口和堆栈跟踪
  • 尝试用属性[STAThread]装饰[STAThread]
  • @Crowcoder [STAThred] 也给出了相同的输出。
  • 您很可能还必须防止程序过早退出。在 WinForms 应用程序中,您会自动拥有它。如果您没有某种循环或等待句柄,控制台程序将退出...
  • 正如@Fildor 所说,阻止应用退出。如果在调用tst.DoWorks() 之后添加Console.ReadLine() 语句会发生什么

标签: c# .net speech-recognition text-to-speech speech-synthesis


【解决方案1】:

您会看到调试器发出RaceOnRCWCleanup。原因可能是您正在实例化但没有正确清理 SpeechSynthesizer 和/或 SpeechRecognitionEngine 在后台创建的 COM 对象。

同时,控制台应用程序不会自动“保持活动状态”。您需要专门添加代码以防止它立即退出。

你需要做两件事:

  • 确保您的应用程序保持足够长的生命周期(例如,通过在 Main 方法中添加 Console.ReadLine() 语句
  • 确保正确清理资源。 SpeechRecognitionEngine 和 SpeechSynthesizer 都实现了IDisposable,因此在不再需要它们时应该将它们丢弃。要正确执行此操作,请在您的测试器类中实现 IDisposable

例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 2017-02-07
    • 2017-10-14
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 2018-07-01
    相关资源
    最近更新 更多