Hello all!
I have an application consisting of two windows, one communicates to the other and sends it a struct constaining two integers (In this case two rolls of a dice).
I will be using events for the following circumstances:
- Process a sends data to process b, process b displays data
- Process a closes, in turn closing process b
- Process b closes a, in turn closing process a
I have noticed that if the second process is constantly waiting for the first process to send data then the program will be just sat waiting, which is where the idea of implementing threads on each process occurred and I have started to implement this already.
The problem i'm having is that I don't exactly have a lot of experience with threads and events so I'm not sure of the best way to actually implement what I want to do.
I'm trying to work out how the other process will know of the event being fired so it can do the tasks it needs to do, I don't understand how one process that is separate from another can tell what the states the events are in especially as it needs to act as soon as the event has changed state.
Thanks for any help
Edit:
I can only use the Create/Set/Open methods for events, sorry for not mentioning it earlier.
Furthermore, I create a new thread in process A which lets the user interact with the application whilst listening for the close event.
Create thread:
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 on 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 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 the event to signalled when B closes:
case WM_DESTROY:
{
SetEvent(hConsumerCloseEvent);
PostQuitMessage(0);
break;
}
As you can see when the Event is signalled, application A is set to close. When I run both application and close process B, process A does not notice the changed signal and does not close.
Edit 2:
After using the GetLastError(); I was able to identify that the handle to the OpenEvent was NULL, the error given is
ERROR_FILE_NOT_FOUND - 2 :The system cannot find the file specified
Is my method of creating the event and reading it incorrect, I've made sure to include the Global\ prefix.