语音处理时出现TargetInvocation异常 [英] TargetInvocation Exception while working on Speech

查看:77
本文介绍了语音处理时出现TargetInvocation异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行代码时收到TargetInvocation异常.我正在研究语音识别项目.我无法找到其背后的原因,但我想可能是由于调用了语音处理程序造成的.以下是模块.

I am getting TargetInvocation Exception while running my code. I am working over Speech Recognition project. I am unable to find the cause behind it but I guess its because of the call to the speech handler. Following is the module.

try
           {
               InitializeComponent();
               textBox1.Focus();
               recog.Enabled = true;
               GrammarBuilder grammar = new GrammarBuilder();
               grammar.Append(new Choices("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
                   "backspace", "point", "equals", "plus", "minus", "multiplied by", "divided by", "LOAN"));
               recog.LoadGrammar(new Grammar(grammar));
               recog.SpeechDetected += recog_SpeechDetected;
               recog.SpeechRecognized += recog_SpeechRecognized;
               recog.SpeechRecognitionRejected += recog_SpeechRejected;
               recog.SpeechHypothesized += recog_SpeechHypothesized;
             //  throw new TargetInvocationException("Inner Exception");
           }
           catch (Exception e)
           {
               //MessageBox.Show(" " +e.Message);
               MessageBox.Show("Message: " + e.Message);
               MessageBox.Show("Source: " + e.Source);
               MessageBox.Show("Target: " + e.TargetSite);
               MessageBox.Show("Stack: " + e.StackTrace);
           }


有时会显示堆栈跟踪.任何建议将不胜感激.



提供错误的MDIContainer [


It sometimes displays the stacktrace. Any suggestion will be appreciated.



MDIContainer giving error[^]

推荐答案

请参阅我对答案的评论并提供其他信息.同时,请执行以下操作:

创建线程并在该线程中运行您的代码.在启动线程之前,请执行以下操作:

Please see my comment to the answer and provide additional information. In the meanwhile, do the following:

Create thread and run your code in this thread. Before starting the thread, do the following:

System.Threading.Thread myThread = new System.Threading.Thread(myRecognizerTest);

myThread.SetApartmentState(System.Threading.ApartmentState.STA);
//or
//myThread.SetApartmentState(System.Threading.ApartmentState.MTA);

myThread.Start();



请尝试使用STA,然后更改代码并尝试使用MTA.

至少有两种不同的识别器类型,它们对线程单元状态的要求不同.此实验将向您显示这是否是问题所在.如果是这样,您将需要选择适当的识别器类型以匹配您的应用程序主线程设置(您可能还希望更改正在使用的UI库,由您自己决定)或在其中运行所有识别器代码使用上面显示的代码,使用适当的单元模型创建单独的线程.

由于您没有提供足够的信息,我现在不想进一步详细介绍.

您最好根据我的想法自行解决.如果这对您没有帮助,请报告发生的情况并提供其他信息;评论此解决方案以通知我-我将尝试提供进一步的帮助.可能是问题有所不同,但是我成功地尝试了不同识别类型,公寓状态和UI库的语音识别,因此我将向您展示有效的组合.只有我不想浪费时间提前显示所有内容.所以,现在轮到您了吗?


好吧,到目前为止您还没有像我希望的那样合作.没关系,让我们尝试另一种方式...
在单独的答案中查看更多详细信息.

—SA



Please try with STA and then change code and try with MTA.

There are at least two different recognizer types, they have different requirement for thread apartment state. This experiment will show you if this is the problem. If it is, you will need either to select appropriate recognizer type to match you application main thread setting (you may also want to change UI library you''re using, it''s up to you) or run all you recognizer code in a separate thread with appropriate apartment model using the code I''ve shown above.

I don''t want to go into further detail right now, as you did not provide enough information.

You better sort it out yourself using my idea. In case this is does not help you, please report what''s going on and provide additional information; comment this solution to give me notification — I''ll try to provide further help. May be the problem is different, but I successfully tried speech recognition in different recognized types, apartment states and UI libraries, so I''ll be able to show you valid combinations. Only I don''t want waste time on showing them all in advance. So, it''s your turn now, deal?


Well, you did not cooperate as I hoped so far. It''s OK, let''s try the other way…
See more details in a separate answer.

—SA


您没有回答我的问题,所以我在解释我的想法.我还在帮你!

您可以使用以下类型:System.Speech.Recognition.SpeechRecognitionEngine.它只能与System.Threading.ApartmentState.MTA一起使用.这种类型也更加通用:提供访问权限,以运行Windows桌面系统上找到的任何正确安装的语音识别服务.

另一种类型是System.Speech.Recognition.SpeechRecognizer.它的限制更大:提供对Windows桌面上可用的默认共享语音识别服务的访问".据我所知,它仅适用于System.Threading.ApartmentState.STA.您不能在运行时在主线程中更改单元(有充分的理由).

对于主线程,您可以在入口点(通常是"main")通过属性定义Apartment状态.它是醚[System.MTAThread][System.STAThread].控制台应用程序可以使用它们之一,但不能使用UI.如果UI和识别器类型的要求不匹配,则只能执行以下变通方法:在单独的线程中运行所有识别代码,并在线程启动前调整单元模型:

You did not answer my questions, so I''m explaining my idea. I''m still helping you!

You might use this type: System.Speech.Recognition.SpeechRecognitionEngine. It can only work with System.Threading.ApartmentState.MTA. This type is also more universal: "Provides access to run any properly installed speech recognition services found on a Windows Desktop system.

Another type is System.Speech.Recognition.SpeechRecognizer. It is more limiting: "Provides access to the default shared speech recognition service available on the Windows Desktop". As far as I remember, it works only with System.Threading.ApartmentState.STA. You cannot change apartment during run-time in your main thread (for a good reason).

For the main thread, you define the Apartment state by the attribute at your entry point (usually "main"); it''s ether [System.MTAThread] or [System.STAThread]. Console application can use either of them, but not UI. If there is no match between requirements of UI and recognizer type, you can only do the following work-around: run all recognition code in a separate thread and adjust the apartment model before thread starts:

System.Threading.Thread myThread = new System.Threading.Thread(myRecognizerTest);
 
myThread.SetApartmentState(System.Threading.ApartmentState.STA);
//or
//myThread.SetApartmentState(System.Threading.ApartmentState.MTA);

myThread.Start();



可能会有些麻烦.

现在,您需要查看您正在使用的识别器类型(如果对您没有多大影响,请更改它的类型)以在具有所需单元状态的线程中运行识别器.输入允许您组合.现在,根据上述要求找出要使用的人.

我不能确定这是您唯一的问题.我只是在我的代码上测试了组合,然后知道哪种组合有效.

—SA



That could be a certain hassle.

Now, you need to see what recognizer type you''re using (or change it if it does not matter much for your) to run the recognizer in a thread with the required apartment state. In gives you allowed combinations. Now figure out yourself which one to use based on the above requirements.

I cannot be really sure that this is your only problem. I just tested the combinations on my code and know which ones work.

—SA


这篇关于语音处理时出现TargetInvocation异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆