简体   繁体   English

在应用程序之间重复WaitForSingleObject的消息中

[英]repeating WaitForSingleObject in messages between to applications

I created two programs in C (producer and consumer) which send messages the one to each other (using CreateEvent, SetEvent and WaitForSingleObject) 我用C创建了两个程序(生产者和使用者),它们互相发送消息(使用CreateEvent,SetEvent和WaitForSingleObject)

I implement one thread for each program to manages this messages using WaitForSingleObject(myEvent, INFINITE) waiting for a message 我使用等待消息的WaitForSingleObject(myEvent,INFINITE)为每个程序实现一个线程来管理此消息

I put the WaitForSingleObject in looping to repeat and obtain many messages in time. 我将WaitForSingleObject放入循环中以重复并及时获取许多消息。

This works fine in the first time but the other times the WaitForSingleObject not wait for a message and the left code run in infinitive loops (including again the WaitForSingleObject) 这工作正常,在第一时间,但其他时间不WaitForSingleObject的等待消息,并在不定式循环左侧的代码运行(再次包括WaitForSingleObject的)

I tested to remove CloseHandle(myEvent) both of two programs but without results 我测试了删除CloseHandle的(myEvent)两者的两个方案,但没有结果

do {
//wait until the event received from another process
WaitForSingleObject(myrcvEvent, INFINITE);

//send the feeback event, if it is failed show a message
printf ("consumer: try to send the feedback event\n");
if (!SetEvent(myfeedbackEvent)) {
msgbox("consumer: Set feedbackEvent failed!");
        return;
 } else {

InvalidateRect(hWnd,NULL,TRUE); //redraw with new values
 }


//CloseHandle(myrcvEvent);
//CloseHandle(myfeedbackEvent);

Sleep(100);
} while(1);

What is wrong? 怎么了? Thanks 谢谢

Why are you using an event for this? 您为什么为此使用事件? Use a semaphore - CreateSemaphore() API. 使用信号量-CreateSemaphore()API。

Even when using an, (inappropriate), event for this signaling, I would not expect continual looping as a symptom with one producer, one consumer and an auto reset event - post your code. 即使在为此信号使用(不适当的)事件时,我也不会期望持续循环是一个生产者,一个使用者和一个自动重置事件的症状-发布您的代码。

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

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