[英]Does Speech Recognition using .NET Framework require a message pump?
I'm writing a plugin (dll file), and I'm creating a WinForm as its interface/dialog. 我正在编写一个插件(dll文件),并且正在创建WinForm作为其界面/对话框。 If it does require a message pump, how and where should I create one?
如果确实需要消息泵,我应该如何在哪里创建消息泵?
SpeechRecognitionEngine is a wrapper around a apartment-threaded COM server. SpeechRecognitionEngine是围绕单元线程COM服务器的包装器。 Yes, a hard requirement for them is at least one thread that is STA and pumps a message loop.
是的,对它们的硬性要求是至少一个线程是STA,并泵送消息循环。 Since you are writing a library, you can't control what your client selects.
由于您正在编写库,因此无法控制客户端选择的内容。 But you can tell her that there's a problem instead of just having your speech recognizer deadlock.
但是您可以告诉她,这不仅仅是问题,而不仅仅是语音识别器陷入僵局。 Add this check to your class constructor:
将此检查添加到您的类构造函数中:
if (System.Threading.Thread.CurrentThread.GetApartmentState() !=
System.Threading.ApartmentState.STA) {
throw new InvalidOperationException("UI thread required");
}
The check is a bit heavy-handed, the recognizer will still work if it is created on a worker thread in a program that also has a UI thread. 这项检查有点繁琐,如果在具有UI线程的程序中的辅助线程上创建了识别器,它仍然可以工作。 Although that mode is quite undesirable, every single call to the recognizer will get marshaled and any events you generate will have to be marshaled by the client.
尽管这种模式是非常不希望的,但是每次对识别器的调用都会被封送,并且您生成的任何事件都必须由客户端封送。 I'd suggest an argument to your main class constructor that allows the client to indicate that she really does want the recognizer to run on a thread.
我建议一个参数传递给你的主类的构造函数,它允许客户端,以表明她确实想识别器上的线程上运行。
您不必创建一个,WinForm应用程序只需创建一个。
If you are creating a Winforms application in the usual manner, it will create its own message pump. 如果您以通常的方式创建Winforms应用程序,它将创建自己的消息泵。 That's all you should need.
这就是您所需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.