title: myProcess.MainWindowHandle = IntPtr.Zero for a process with a form that is visible? [C#]
body: <p>I have a windows service (Serv.exe) running as LocalSystem, at a certain point it wants to show the logged-on user that it is processing data, therefore I CreateProcessAsUser(Display.exe) which is a C# Windows Form that display information (this all works perfectly fine).</p>
  
  <p>Now, when the Service is finished I want to close Display.exe - from reading around there are 2 general options. Either kill the process (which works fine in my case, but is not recommended) or use the myProcess.CloseMainWindow(); which I am currently trying to implement.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>            Process[] myProcesses;
              myProcesses = Process.GetProcessesByName("Display");
              foreach (Process myProcess in myProcesses)
              {
                  if (myProcess.MainWindowHandle == IntPtr.Zero)
                  {
                      myProcess.Kill();
                  }
                  else
                  {
                      myProcess.CloseMainWindow();
                  }
              }
  </code></pre>
  
  <p>Now, Display is simply a C# Windows Form application, I can see it on screen and my code is able to find the process (as killing it works fine) - but why is myProcess.MainWindowHandle == IntPtr.Zero? I've tried adding 15 second sleep (incase the form was still loading) but it made no difference.</p>
  
  <p>Is there something I need to implement in Display.exe itself to process the .CloseMainWindow()? Can this have something to do with the fact that my Service (Serv.exe) running as LocalSystem is trying to get the MainWindowHandle for a process running under a different user?</p>
  
  <p>Any help would be appreciated.
  Thanks,</p>