如何有效地实现事件循环?

时间:2021-12-31 14:31:59

COM Object (Server) sends event notification successfully to COM Client

COM对象(服务器)成功向COM客户端发送事件通知

Without:

  • ATL
  • MFC

How to efficiently get the main thread to wait/sleep (infinitely) until COM Server notifies the COM Client of a particular event?

如何有效地让主线程等待/休眠(无限),直到COM服务器通知COM客户端特定事件?

1 个解决方案

#1


With event objects.

使用事件对象。

The main thread calls CreateEvent() in its initialisation to create an auto-reset event object.

主线程在其初始化时调用CreateEvent()以创建自动重置事件对象。

The main thread then enters an event loop in which it calls MsgWaitForMultipleObjects() repeatedly. (here is an example of a message loop.)

然后主线程进入一个事件循环,在该循环中重复调用MsgWaitForMultipleObjects()。 (这是一个消息循环的例子。)

And you generally do need to check for window messages, even if the main thread has no GUI.

而且您通常需要检查窗口消息,即使主线程没有GUI。

In the client thread (the one that creates the sink object) call SetEvent() within the sink method, after any necessary state update. This will wake up the main thread.

在客户端线程(创建接收器对象的线程)中,在任何必要的状态更新之后,在接收器方法中调用SetEvent()。这将唤醒主线程。

And read this and this, if you haven't already.

如果你还没有,请阅读本文。

#1


With event objects.

使用事件对象。

The main thread calls CreateEvent() in its initialisation to create an auto-reset event object.

主线程在其初始化时调用CreateEvent()以创建自动重置事件对象。

The main thread then enters an event loop in which it calls MsgWaitForMultipleObjects() repeatedly. (here is an example of a message loop.)

然后主线程进入一个事件循环,在该循环中重复调用MsgWaitForMultipleObjects()。 (这是一个消息循环的例子。)

And you generally do need to check for window messages, even if the main thread has no GUI.

而且您通常需要检查窗口消息,即使主线程没有GUI。

In the client thread (the one that creates the sink object) call SetEvent() within the sink method, after any necessary state update. This will wake up the main thread.

在客户端线程(创建接收器对象的线程)中,在任何必要的状态更新之后,在接收器方法中调用SetEvent()。这将唤醒主线程。

And read this and this, if you haven't already.

如果你还没有,请阅读本文。