title: How to close a HTA window from another HTA?
body: <p>I've tried to implement my own modal dialogs (<a href="https://stackoverflow.com/q/11727815/1169519">Why?</a>). I open a new window using <code>WScript.Shell</code>:</p>
  
  <pre><code>dlg = shell.Run(dlgPath, 1, true);
  </code></pre>
  
  <p>The line above creates a "semi-modal" HTA window. The script execution is stopped at this line, but the main window is still responsive. This issue is tackled within <code>onfocusin</code> eventhandler, which returns focus back to the new HTA window using <code>shell.AppActivate()</code>. This prevents even "Close Window" button to close the main window. However, in Windows7 there's a back gate to close the main window, the icon in Windows Task Bar.</p>
  
  <p>If the main window is closed, a decent modal dialog should be closed as well. In this case it is not closed, since the opened HTA window is not a real child window of the main.</p>
  
  <p>I can close this dialog emulator within main window's <code>onbeforeunload</code> handler function below. </p>
  
  <pre><code>beforeTopClose = function () {
      var wmiLocator = new ActiveXObject('WbemScripting.SWbemLocator'),
          wmiService = wmiLocator.ConnectServer('.', 'root\\CIMV2'),
          htas = new Enumerator(wmiService.ExecQuery("Select * from Win32_Process Where name = 'mshta.exe'"));
      while (!htas.atEnd()) {
          if (htas.item().CommandLine.indexOf('_tools\\dlgbase') &gt; -1) {
              htas.item().Terminate(0);
          }
          htas.moveNext();
      }
      return;
  }
  </code></pre>
  
  <p>Now everything looks good, there are no windows of my app left on the screen. However, when I open Windows Task Manager Processes -tab, I can see the process of the <em>main window</em> still running.</p>
  
  <p>Why the main window is still running? Is it waiting for the end of the <code>shell</code>-process? What should I do in <code>onbeforeunload</code> handler to get the main window closed totally? If I need a different approach to this, please let me know.</p>