0

I've created an add-in for a program that starts up a console using the following code block below however I don't know how to close the console window without closing the entire program. I've found this example but when I try Environment.Exit(0) it closes my entire program.

I've been on this for a couple of days however I'm not having any luck with this. Long story short, I am creating the console to pipe information between it and another process. Once it receives the information, I'd like it to close and resume to the parent process. I am sure this is an easy thing to but I'm not finding the solution on my own.

Could someone point me in the right direction? Thank you in advance.

private void Form1_Load(object sender, EventArgs e) 
{ 
    AllocConsole(); 
} 

[DllImport("kernel32.dll", SetLastError = true)] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern bool AllocConsole(); 

Cflux
  • 1,423
  • 3
  • 19
  • 39
  • 1
    You should be able to call `FreeConsole()` to release the console window. See duplicate for details. That said, I find that alloc/free console is generally a very poor way to do this sort of thing. It's not clear from your question why you are creating the console, but more likely you should just be using the `Process` class and redirecting stdout, stderr, and stdin as needed. – Peter Duniho Jul 19 '20 at 17:54
  • @PeterDuniho, The `FreeConsole()` worked. I tried using the `Process` class but I was having issues with it. I'll give it another shot. I'm creating a console to pipe information back and forth between to open programs on a single computer using named pipes. kinda like making Excel and Word talk to each other while both are open. Something similar to [this](https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-use-named-pipes-for-network-interprocess-communication) – Cflux Jul 20 '20 at 03:35

0 Answers0