使用 .NET Framework 的语音识别是否需要消息泵?

发布于 2024-08-28 09:24:33 字数 82 浏览 6 评论 0原文

我正在编写一个插件(dll 文件),并且正在创建一个 WinForm 作为其界面/对话框。 如果它确实需要消息泵,我应该如何以及在哪里创建一个消息泵?

I'm writing a plugin (dll file), and I'm creating a WinForm as its interface/dialog.
If it does require a message pump, how and where should I create one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

五里雾 2024-09-04 09:24:34

您不必创建一个,WinForm 应用程序已经有一个。

You don't have to create one, the WinForm application has one.

A君 2024-09-04 09:24:34

如果您以通常的方式创建 Winforms 应用程序,它将创建自己的消息泵。这就是你所需要的。

If you are creating a Winforms application in the usual manner, it will create its own message pump. That's all you should need.

蓦然回首 2024-09-04 09:24:33

SpeechRecognitionEngine 是公寓线程 COM 服务器的包装器。是的,对它们的硬性要求是至少有一个线程是 STA 并泵送消息循环。由于您正在编写一个库,因此您无法控制客户的选择。但您可以告诉她出现了问题,而不仅仅是让您的语音识别器陷入僵局。将此检查添加到您的类构造函数中:

  if (System.Threading.Thread.CurrentThread.GetApartmentState() !=
      System.Threading.ApartmentState.STA) {
    throw new InvalidOperationException("UI thread required");
  }

该检查有点严厉,如果识别器是在也具有 UI 线程的程序中的工作线程上创建的,则该识别器仍然可以工作。尽管这种模式是非常不可取的,但对识别器的每次调用都将被整理,并且您生成的任何事件都必须由客户端整理。我建议您的主类构造函数使用一个参数,该参数允许客户端表明她确实希望识别器在线程上运行。

SpeechRecognitionEngine is a wrapper around a apartment-threaded COM server. Yes, a hard requirement for them is at least one thread that is STA and pumps a message loop. 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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文