title: How to send Event signal through Processes - C
body: <p>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).</p>
  
  <p>I will be using events for the following circumstances:</p>
  
  <ul>
  <li>Process a sends data to process b, process b displays data</li>
  <li>Process a closes, in turn closing process b</li>
  <li>Process b closes a, in turn closing process a</li>
  </ul>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>Thanks for any help</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I can only use the Create/Set/Open methods for events, sorry for not mentioning it earlier.</p>
  
  <p><strong>Furthermore</strong>, I create a new thread in process A which lets the user interact with the application whilst listening for the close event.</p>
  
  <p><strong>Create thread:</strong></p>
  
  <pre><code>hCreateEventThread = CreateThread(
                  NULL,       // lpThreadAttributes (default)
                  0,          // dwStackSize (default)
                  ThreadFunc, // lpStartAddress
                  NULL,       // lpParameter
                  0,          // dwCreationFlags
                  &amp;hCreateEventThreadID   // lpThreadId (returned by function)
                  );
  
              if(hCreateEventThread != NULL)
              {
                  MessageBox(hMainWindow,L"Thread created!",L"Success!",MB_OK);
              }
  </code></pre>
  
  <p><strong>Opening event on A when B closes</strong>:</p>
  
  <pre><code>    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;
              }
          }
      }
  </code></pre>
  
  <p><strong>Creating event in b  (In WM_CREATE):</strong></p>
  
  <pre><code>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);
              }
  </code></pre>
  
  <p><strong>Setting the event to signalled when B closes:</strong></p>
  
  <pre><code>case WM_DESTROY:
          {
              SetEvent(hConsumerCloseEvent);
              PostQuitMessage(0);
              break;
          }
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>After using the GetLastError(); I was able to identify that the handle to the OpenEvent was NULL, the error given is </p>
  
  <blockquote>
    <p>ERROR_FILE_NOT_FOUND - 2 :The system
    cannot find the file specified</p>
  </blockquote>
  
  <p>Is my method of creating the event and reading it incorrect, I've made sure to include the Global\ prefix.</p>