title: Can't get Event notification between processes in winapi
body: <p>I'm learning about Events synchronization in winapi. I took as example 2 processes 1 that creates, sets an event. Also it opens the 2nd process. The 2nd program that waits to be notified and print a message. Here is the code:
  Program 1:</p>
  <pre><code>#define _CRT_SECURE_NO_WARNINGS
  #include &lt;Windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  
  HANDLE hEvent;
  
  int main()
  {
      hEvent = CreateEvent(NULL, TRUE, FALSE, &quot;Ev&quot;);
      if (hEvent == NULL) {
          printf(&quot;Error event&quot;);
          return -1;
      }
  
      PROCESS_INFORMATION pi;
      STARTUPINFO si;
      memset(&amp;si, 0, sizeof(si));
      si.cb = sizeof(si);
  
      if (!SetEvent(hEvent)) {
          printf(&quot;Setevent error\n&quot;);
          return -1;
      }
  
      if (!CreateProcess(&quot;G:\\testing\\test1_2\\x64\\Debug\\test1_2.exe&quot;, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &amp;si, &amp;pi)) {
          printf(&quot;Cannot create process.\n&quot;);
          return -1;
      }
  
      CloseHandle(hEvent);
  }
  </code></pre>
  <p>Program 2:</p>
  <pre><code>#define _CRT_SECURE_NO_WARNINGS
  #include &lt;Windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  
  HANDLE hEvent;
  
  int main()
  {
      hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, &quot;Ev&quot;);
  
      DWORD res;
      while (true)
      {
  
          res = WaitForSingleObject(hEvent, INFINITE);
          if (res == WAIT_OBJECT_0) {
              printf(&quot;Hello world!\n&quot;);
              break;
          }
      }
      CloseHandle(hEvent);
      return 0;
  }
  </code></pre>
  <p>I can't make it work to print the text. Any ideas what am I doing wrong? Thanks in advance!</p>
  <p><strong>EDIT :</strong> As I can't answer this question I found the issue. Please check comments bellow and follow the link that @RaymondChen provided. Change <code>EVENT_MODIFY_STATE</code> to <code>EVENT_MODIFY_STATE | SYNCHRONIZE</code> and use a <code>Sleep</code> (first process is dying and Event disappear as @RbMm pointed out) after <code>CreateProcess</code>.</p>