I have an application consisting of two windows, one communicates with the other and sends it a structure containing two integers (in this case, two rolls of bone).
I will use events for the following circumstances:
- Process send data for processing b, process b displays data
- The process closes, in turn, the closing process b
- Process b closes a, in turn, closes a
I noticed that if the second process is constantly waiting for the first process to send data, then the program will just sit waiting, as a result of which the idea of โโimplementing threads for each process came up, and I started to implement it already.
The problem I am facing is that I do not have much experience with streams and events, so I am not sure that you can really implement what I want to do.
Iโm trying to understand how another process will know about the dismissed event, so that it can carry out the tasks that it should perform, I donโt understand how one process, which is separated from another, can say that the states of the event occur in particular, since it must act as soon as the event has changed.
Thanks for any help
Edit:
I can only use Create / Set / Open methods for events, sorry for not mentioning this before.
In addition , I create a new thread in process A, which allows the user to interact with the application while listening to the close event.
Create stream:
hCreateEventThread = CreateThread( NULL, // lpThreadAttributes (default) 0, // dwStackSize (default) ThreadFunc, // lpStartAddress NULL, // lpParameter 0, // dwCreationFlags &hCreateEventThreadID // lpThreadId (returned by function) ); if(hCreateEventThread != NULL) { MessageBox(hMainWindow,L"Thread created!",L"Success!",MB_OK); }
Opening event A when B closes :
DWORD WINAPI ThreadFunc(LPVOID passedHandle) { hConsumerCloseEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, TEXT("Global\\ConsumerCloseEvent")); while(TRUE) { dwCloseResult = WaitForSingleObject(hConsumerCloseEvent,INFINITE); switch (dwCloseResult) { // State of object is signalled case WAIT_OBJECT_0: //Consumer has closed, exit program. //CloseHandle(hDiceRoll); //CloseHandle(hCloseEvent); //CloseHandle(hCreateEventThread); ExitProcess(1); break; default: return; } } }
Creating an event in b (In WM_CREATE):
hConsumerCloseEvent = CreateEvent( NULL, // default security attributes TRUE, // manual-reset event TRUE, // initial state is nonsignaled TEXT("Global\\ConsumerCloseEvent") // object name ); if(hConsumerCloseEvent == NULL) { MessageBox(hMainWindow,L"CreateEvent failed",L"Error",MB_OK); }
Setting an event for an alarm when B closes:
case WM_DESTROY: { SetEvent(hConsumerCloseEvent); PostQuitMessage(0); break; }
As you can see, when the event is signaled, application A is set to close. When I start both applications and close process B, process A does not notice the changed signal and does not close.
Edit 2:
After using GetLastError (); I was able to determine that the OpenEvent descriptor is NULL, the error indicated
ERROR_FILE_NOT_FOUND - 2: the system cannot find the specified file
Whether my method of creating an event and reading it is incorrect, I have definitely included the Global \ prefix.