L
L
lixaka2014-12-03 01:15:56
Windows
lixaka, 2014-12-03 01:15:56

Windiws - How to create 2 processes where one fires events and the other recognizes them?

Two processes are created. Process A randomly, with an interval of 1 s, activates one of two events. Process B recognizes these events and displays them on the screen as the letters X or Y. When the number of issued letters reaches 100, process B tells A that it's time to exit.
Code that, according to my logic:
Process A
#include "stdafx.h"
#include
int main(int argc, char* argv[])
{
STARTUPINFO si;
PROCESS_INFORMATIONpi;
HANDLE hEvent1 = CreateEvent(NULL,FALSE,FALSE,NULL);
HANDLE hEvent2 = CreateEvent(NULL,FALSE,FALSE,NULL);
HANDLE hEvent3 = CreateEvent(NULL,FALSE,FALSE,NULL);
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
if (!CreateProcess(L"C:\\Users\Documents\Visual Studio 2010\Projects\B\Debug\B.exe", NULL, NULL, NULL, FALSE,
CREATE_NEW_CONSOLE, NULL, NULL, &si, π))
/ / close the handles of this process
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
intn=0;
while (n<=100)
{
DWORD WaitForSingleEvent (HANDLE, DWORD dwMilliseconds);
BOOL SetEvent((hEvent1)||(hEvent2));
WaitForSingleObject(hEvent3, 0);
DWORD dwWaitResult = WaitForSingleObject(hEvent3, INFINITE);
if (dwWaitResult != WAIT_OBJECT_0)
{
return 0;
}
n++;
}
}
Process B
#include "stdafx.h"
#include
#include
int main(int argc, char* argv[])
{
HANDLE hEvent1 = OpenEvent(NULL,FALSE,NULL);
HANDLE hEvent2 = OpenEvent(NULL,FALSE,NULL);
HANDLE hEvent3 = OpenEvent(NULL,FALSE,NULL);
intk=0;
while (k<100)
{
WaitForSingleObject( hEvent1, INFINITE);
WaitForSingleObject( hEvent2, INFINITE);
if (hEvent1!=NULL)
{
std::cout<<"X";
}
if (hEvent2!=NULL)
{
std::cout<<"Y";
}
k++;
}
if (k=100)
{
SetEvent(hEvent3);
return 1;
}
}
I'm just starting to analyze this topic, but I can't even check if it works correctly. It seems that, logically, I launch a signal - I wait - I recognize it, I give it to another process, can anyone help with this question?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Armenian Radio, 2014-12-03
@gbg

The direction is correct, but the implementation is lame.
You need to create named events (the last parameter for CreateEvent).
You can wait for one of many events using WaitForMultipleObjects - it returns WAIT_OBJECT_0+number of the first exposed event.
Go ahead.

L
lixaka, 2014-12-06
@lixaka

Thank you very much! You can also ask such a question, here "Process A in a random order, with an interval of 1 s, activates one of two events." Is it possible to implement this with the line
BOOL SetEvent((hEvent1)||(hEvent2));
?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question