简体   繁体   English

使用Windows消息循环在我正在编写的库中接收事件

[英]Use Windows message loop to receive an event in a library I'm writing

I'm writing a library that wraps some of Media Foundation functionality. 我正在写一个包装一些Media Foundation功能的库。 I want to be able to notify a library user through a callback of when a webcam is connected/disconnected to/from the system. 我希望能够通过回调将网络摄像头何时连接到系统/从系统断开连接来通知图书馆用户。 MSDN describes how to know when a camera is disconencted , but it uses message loop to let you know of that. MSDN描述了如何知道何时断开摄像机 ,但它使用消息循环让您知道这一点。 I don't know Windows message loops too well, but what I have read in this MSDN article tells me that I must have a window in order to have a message loop, which is unacceptable for a library. 我不太了解Windows消息循环,但是我在MSDN文章中读到的内容告诉我,必须有一个窗口才能有消息循环,这对于库是不可接受的。

So, I have several questions: 因此,我有几个问题:

  1. Can I create a message loop in a new thread and receive those notification messages described by the first link? 我可以在新线程中创建消息循环并接收第一个链接描述的那些通知消息吗? (I want it to be in a new thread so that it doesn't block the library user's thread then the library user calls setCameraChangeCallback(...) , which starts the message loop inside.) If so, which functions for creating message loop should I use? (我希望它位于新线程中,以便不阻塞库用户的线程,然后库用户调用setCameraChangeCallback(...) ,这将在内部启动消息循环。)如果是这样,则用于创建消息循环的功能我应该使用吗?

  2. Can I do that without creating any windows? 我可以在不创建任何窗口的情况下做到这一点吗? It's a library, so it would be very odd if a library user called setCameraChangeCallback(...) and a window suddenly appeared. 这是一个库,所以如果一个库用户叫setCameraChangeCallback(...)并突然出现一个窗口,那将是很奇怪的。 Again, an explanation of how to do that (function names, specific arguments to use, etc.) is very welcome. 同样,非常欢迎您提供有关如何执行此操作的说明(函数名称,要使用的特定参数等)。

  3. Can my library be used without issues in a Windows application? 我的库可以在Windows应用程序中正常使用吗? Meaning that a Windows application that would be using my library is likely to already have a window created and its own message loop running. 这意味着将要使用我的库的Windows应用程序可能已经创建了一个窗口,并且正在运行其自己的消息循环。 Would my message loop running in the separate thread interfere with library user's message loop? 我在单独线程中运行的消息循环是否会干扰库用户的消息循环? If so, how to avoid that? 如果是这样,如何避免这种情况?

  4. Does anything stop me from creating two or more threads with message loops, each registered to get notified for the camera change event? 有什么阻止我创建带有消息循环的两个或多个线程的消息循环,每个线程都已注册以获取有关相机更改事件的通知?

This MSDN article tells me that I must have a window in order to have a message loop, which is unacceptable for a library. 这篇MSDN文章告诉我,必须有一个窗口才能有消息循环,这对于库是不可接受的。

Not so. 不是这样 Create a message only window , or even just a hidden window. 创建仅消息窗口 ,甚至创建一个隐藏窗口。 Let that window receive the notification messages and then forward them. 让该窗口接收通知消息,然后转发它们。

You can choose to do this inside a dedicated thread if you wish, or not. 如果愿意,可以选择在专用线程内执行此操作。 Whichever thread executed the code that creates the window is deemed to be the owning thread of the window. 执行创建窗口的代码的任何线程都被视为窗口的拥有线程。 Messages are sent to that thread. 消息发送到该线程。 That thread must dispatch messages in order for the window to receive them. 该线程必须调度消息,以便窗口接收消息。

On the face of it, you may think that it will just be cleaner to create the window in a dedicated thread and so isolate it from the host application. 从表面上看,您可能会认为在专用线程中创建窗口并将其与主机应用程序隔离会更干净。 That has benefits, but the cost is that you need to consider what happens when you wish to forward notification events. 这样做有好处,但代价是您需要考虑当希望转发通知事件时会发生什么。 The messages that you receive will arrive in your dedicated thread. 您收到的消息将到达您的专用线程。 If you forward events directly then your library's host will end up executing code asynchronously in your dedicated thread. 如果直接转发事件,则您的库的主机最终将在专用线程中异步执行代码。 Is that really what you want? 那真的是你想要的吗?

A more common approach would see the events being fired in the host thread that originally requested to receive notifications. 一种更常见的方法是在最初请求接收通知的主机线程中触发事件。 That would mean your library would have to require that the host application dispatched messages. 这意味着您的库必须要求主机应用程序分发消息。

Clearly you have some choices to make here. 显然,您可以在这里做出一些选择。 But the bottom line is that you should not believe that library code must not create windows. 但最重要的是,您不应该相信库代码一定不能创建窗口。 Windows need not be visible and indeed message only windows are designed specifically for your usage scenario. Windows不必是可见的,实际上只有消息的窗口是专门为您的使用方案设计的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM