简体   繁体   English

如何使 WaitForSingleObject 在从 main 作为类成员函数调用的线程内接收信号?

[英]How to make WaitForSingleObject receive a signal inside a thread called from main as a class member function?

I have a working piece of code on main function in Windows with C++ like:我在 Windows 中使用 C++ 编写了一段关于 main 函数的工作代码,例如:

some_handle = dll_generate_handle;
while(true) {
    if(WaitForSingleObject(some_handle,100)){
          //CODE I AM INTERESTED ABOUT
    }
}

The signal comes from a dll function.该信号来自一个 dll 函数。 I attempt to port this principle into a framework in the next manner:我尝试以下一种方式将此原则移植到框架中:

  1. I have a main again but this main gets the static instance of a class i1.我又有一个 main ,但是这个 main 获取了类 i1 的静态实例。 This class has a member function "event_checking" doing the same piece of code above.这个类有一个成员函数“event_checking”,执行上面相同的代码。

  2. I can access to this event_checking function through an instance i2 of an intermediate class so I decided to Wrap i1.event_checking with我可以通过中间类的实例 i2 访问此 event_checking 函数,因此我决定将 i1.event_checking 与

    void eventCheckingWrapper(){ i1.event_checking(); }
  3. Still in the main I spawn a thread on eventCheckingWrapper.仍然在主要我在 eventCheckingWrapper 上生成一个线程。 So I am not interested in the thread returning as long as signals arrive.所以我对只要信号到达就返回的线程不感兴趣。 An example of this principle could be:这个原则的一个例子可能是:

     std::thread t(&ClassName::eventCheckingWrapper, &i2);

The thread is running well but no signals are arriving such that the timeout is always reached and the thread starts over in its while loop.线程运行良好,但没有信号到达,因此总是达到超时并且线程在其 while 循环中重新开始。

How can I properly get this to work?我怎样才能正确地让它工作?

I solved the issue.我解决了这个问题。 So everything Remy Lebeau said was true.所以雷米勒博说的都是真的。 In the end the dll had a function to activate the reception of signals through the event handle that I had to call before.最后,dll 有一个函数可以通过我之前必须调用的事件句柄来激活信号的接收。 So in reality no signals were coming but just to be informative what I could confirm after this debugging experience was basically that "parent" threads need to be kept alive in Windows, winapi WaitForSingleObject() keeps the state of an event signaled if it has not been waited for and new re-setting an event does not change its state (remains signaled) as long as no thread has waited for it (after which it changes to unsignaled mode) so time issues do not arise.所以实际上没有信号到来,但只是为了提供信息,我可以在这次调试体验之后确认基本上是“父”线程需要在 Windows 中保持活动状态,winapi WaitForSingleObject() 保持事件的状态,如果它没有一直等待并且重新设置事件不会改变其状态(保持有信号),只要没有线程等待它(之后它更改为无信号模式),因此不会出现时间问题。 As a matter of fact, it should be checked if the events provided by 3d party software care about the thread in which it was created and if therefore there is no problem where the handle is retrieved.事实上,应该检查 3d 方软件提供的事件是否关心创建它的线程,因此在检索句柄时是否没有问题。 Thanks!谢谢!

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

相关问题 如何从 main ( ) 调用线程成员 function - how to call a thread member function from main ( ) 具有来自另一个类的成员函数的类中的线程 - Thread inside a class with member function from another class 如何制作我的QThread块并等待从主线程调用的函数返回值? - How can I make my QThread block and wait for a function called from the main thread to return a value? 如何使用 boost 使 class 成员 function 作为线程 function - How to make class member function as thread function using boost 如何使我的类成员函数无法在main中调用? - How can I make it impossible for my class member functions to be called in main? 如何使用来自另一个 class 的公共成员 function 作为参数调用线程 - How to call thread with a public member function from another class as argument 如何将主 Dlg 的指针从线程传递到 MFC 内部的函数 - How to pass the pointer of main Dlg from a thread to a function inside in MFC 如何确保成员函数被调用一次 - How to make sure a member function is called once 如何从non_GUI类和我们可以在主GUI类中检测到的非GUI线程发出信号 - how to emit a signal from a non_GUI class and from a non-GUI thread which we can detect in main GUI class 如何允许在类声明中定义的成员函数被所述类的常量对象调用 - How to allow a member function that's defined inside the class declaration to be called by constant objects of said class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM