title: Show Form from hidden console application
body: <p>I have a main application which runs a console application. The console application is usually started hidden (<code>ProcessWindowStyle.Hidden</code>), but for testing purposes I can run it with the window shown.</p>
  
  <p>Within the console application I can have plugins loaded and executed. One of the plugins tries to open a WinForm dialog. It works fine if the console application is visible, but it doesn't work any more if the console is hidden.</p>
  
  <p>I have tried:</p>
  
  <pre><code>Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form());
  </code></pre>
  
  <p>and I also tried the same in a new thread.</p>
  
  <pre><code>Thread t = new System.Threading.Thread(start);
  t.Start();
  t.Join();
  </code></pre>
  
  <p>where <code>start()</code> contains the things before. In addition I tried <code>ShowDialog()</code></p>
  
  <pre><code>Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  var f = new Form();
  f.ShowDialog();
  </code></pre>
  
  <p>None of the methods showed the window.</p>
  
  <p>In WinDbg, the native callstack always includes <code>NtUserWaitMessage()</code>:</p>
  
  <pre><code>0:000&gt; k
  ChildEBP RetAddr  
  0038dd58 7b0d8e08 USER32!NtUserWaitMessage+0x15
  </code></pre>
  
  <p>And the managed callstack always includes <code>WaitMessage()</code>, <code>FPushMessageLoop()</code> and <code>RunMessageLoop()</code>:</p>
  
  <pre><code>0:000&gt; !clrstack
  OS Thread Id: 0x47c4 (0)
  ESP       EIP     
  0045e560 76bff5be [InlinedCallFrame: 0045e560] System.Windows.Forms.UnsafeNativeMethods.WaitMessage()
  0045e55c 7b0d8e08 System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32, Int32, Int32)
  0045e5f8 7b0d88f7 System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
  0045e64c 7b0d8741 System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
  0045e67c 7b5ee597 System.Windows.Forms.Application.RunDialog(System.Windows.Forms.Form)
  0045e690 7b622d98 System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window)
  0045e71c 7b622faf System.Windows.Forms.Form.ShowDialog()
  </code></pre>
  
  <p><strong>How can I show a WinForms form from a hidden console window?</strong></p>
  
  <p>SSCCE:</p>
  
  <p>Compile this as a Windows Form application:</p>
  
  <pre><code>public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          var startInfo = new ProcessStartInfo("Console.exe");
          startInfo.WindowStyle = ProcessWindowStyle.Hidden;
          Process.Start(startInfo);
      }
  }
  </code></pre>
  
  <p>Compile this as the console application:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          var mainForm = new Form();
          // Enable next line to make it show
          // mainForm.Visible = true;
          Application.Run(mainForm);
      }
  }
  </code></pre>