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)

I implement one thread for each program to manages this messages using WaitForSingleObject(myEvent, INFINITE) waiting for a message

I put the WaitForSingleObject in looping to repeat and obtain many messages in time.

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)

I tested to remove CloseHandle(myEvent) both of two programs but without results

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 eventn");
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.

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.

链接地址: http://www.djcxy.com/p/30800.html

上一篇: 使用无效句柄调用SetEvent

下一篇: 在应用程序之间的消息中重复WaitForSingleObject