Answer the question
In order to leave comments, you need to log in
Windows - How to create 2 processes in which 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.
Process A
#include "stdafx.h"
#include "process.h"
#include
int main(void)
{
DWORD dwWaitResult;
STARTUPINFO si;
PROCESS_INFORMATIONpi;
HANDLE hEvent1=CreateEvent (NULL, FALSE, FALSE,L"hEvent1");
HANDLE hEvent2=CreateEvent(NULL,FALSE,FALSE,L"hEvent2");
HANDLE hEvent3=CreateEvent(NULL,FALSE,FALSE,L"hEvent3");
ZeroMemory(&si,
si.cb = sizeof(STARTUPINFO);
if (!CreateProcess(L"C:\\B\\Debug\\B.exe", NULL, NULL, NULL, FALSE,CREATE_NEW_CONSOLE, NULL, NULL, &si, π))
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
do
{
HANDLE events[]={hEvent1, hEvent2,hEvent3};
SetEvent(events[rand()&1]);
DWORD dwWaitResult = WaitForSingleObject(hEvent3, INFINITE);
}
while (dwWaitResult!=WAIT_OBJECT_0);
return 0;
}
Process B
#include "stdafx.h"
#include
#include
int main(int argc, char* argv[])
{
HANDLE hEvent1 = OpenEvent(NULL,FALSE,L"hEvent1");
HANDLE hEvent2 = OpenEvent(NULL,FALSE,L"hEvent2");
HANDLE hEvent3 = OpenEvent(NULL,FALSE,L"hEvent3");
intk=0;
while (k<100)
{
HANDLE events[]={hEvent1, hEvent2};
DWORD dwWaitResult = WaitForMultipleObjects(2, events, FALSE, 0);
if (dwWaitResult==WAIT_OBJECT_0)
{
std::cout<<"X";
}
if (dwWaitResult==WAIT_OBJECT_0+1)
{
std::cout<<"Y";
}
k++;
}
if (k==100)
{
SetEvent(hEvent3);
return 0;
}
}
The compiler skips the last parameter in CreatEvent only in this way, the enumeration of different options did not give a result (maybe this is the error?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question