簡體   English   中英

運行用於語音識別的控制台應用程序時的RaceOnRCWCleanup

[英]RaceOnRCWCleanup when running console app for speech recognition

我不知道我們是否可以創建一個控制台應用程序,還是不能創建用於語音識別的(搜索相同的應用程序,但未找到任何答案)並嘗試了此代碼。

我有此代碼在winforms應用程序中工作

但是當嘗試在控制台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());
        }
    }
}

這是錯誤頁面的快照

在此處輸入圖片說明

我還加載了給定的選項“ Microsoft.symbol.Server”,但它仍提供相同的輸出。

編輯

這里是輸出窗口

在此處輸入圖片說明

輸出的長度很大,因此無法顯示所有輸出,因此捕獲了一些相關部分(遺憾)。

您正在看到調試器發出RaceOnRCWCleanup 其原因可能是您正在實例化,但沒有正確清理由引擎蓋下創建COM對象SpeechSynthesizer和/或SpeechRecognitionEngine

同時,控制台應用程序不會自動“保持活動”。 您需要專門添加代碼以防止其立即退出。

您需要做兩件事:

  • 確保您的應用程序存活時間足夠長(例如,通過在Main方法中添加Console.ReadLine()語句Console.ReadLine()
  • 確保正確清理資源。 SpeechRecognitionEngine和SpeechSynthesizer都實現IDisposable ,因此應在不再需要它們時將它們丟棄。 要正確執行此操作,請在您的測試人員類中實現IDisposable

例:

 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