title: Prevent Windows Forms application from closing when the Console is closed
body: <p>I have a standard Windows Forms application and I needed a solution to open the console within the Windows Forms application setting. I came up with this solution: </p>
  
  <pre><code>Public Class Win32
  
      &lt;DllImport("kernel32.dll")&gt;
      Public Shared Function AllocConsole() As Boolean
      End Function
  
      &lt;DllImport("kernel32.dll")&gt;
      Public Shared Function FreeConsole() As Boolean
      End Function
  
  ' ...
  
  Win32.AllocConsole()
  Console.WriteLine("Test")
  </code></pre>
  
  <p>Using the P/Invoke functions above, I can open a console without my application being a "Console Application".</p>
  
  <p>The problem I am now having is that when I close the console window it exits my program and closes all Forms and Windows. Is there a way to prevent the user from closing the Console window, or to prevent the program from exiting when the console window is closed? (I can close the console programmatically using <code>Win32.FreeConsole()</code>.)</p>