C
C#15mo ago
Mekasu0124

✅ Closing first terminal, opening second terminal

// in file A
var p = new Process();
p.StartInfo.FileName = "test.exe";
p.Start();

// in file B
var p = new Process();
p.StartInfo.FileName = "main_app.exe";
p.Start();
// in file A
var p = new Process();
p.StartInfo.FileName = "test.exe";
p.Start();

// in file B
var p = new Process();
p.StartInfo.FileName = "main_app.exe";
p.Start();
My main program in main_app.exe. When I run my main program, it opens in terminal 1 as expected. When main_app.exe runs the new process to start a second program, I would like for that second program to open in a new terminal (terminal 2) and the terminal (terminal 1) to be closed. So close current terminal, open new terminal. Then when the second program finishes it's process and relaunches main_app.exe, I would like for terminal 2 to close, and a new terminal (terminal 3) to open. How would I accomplish this? TL:DR 1. main_app.exe => terminal 1 2. test.exe => terminal 2 3. terminal 1 finishes work, opens terminal 2, closes terminal 1 4. terminal 2 finishes work, opens terminal 3, closes terminal 2
44 Replies
jcotton42
jcotton4215mo ago
why?
Mekasu0124
Mekasu0124OP15mo ago
I have tried
p.StartInfo.Arguments = "/c"; // did nothing
p.StartInfo.Arguments = "/c"; // did nothing
I have also looked at https://stackoverflow.com/questions/3616010/start-command-windows-and-run-commands-inside, https://www.google.com/search?client=opera&q=CreateNoWindow+c%23&sourceid=opera&ie=UTF-8&oe=UTF-8, https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.createnowindow?view=net-7.0 and other sites to find a solution. Nothing works to keep it clean
jcotton42
jcotton4215mo ago
what if I'm running your app from an already open terminal?
Mekasu0124
Mekasu0124OP15mo ago
then when the installer starts, it would close the current window, open a new one => installer does its thing, then closes that window and reopens the console back to main application it's no different than any other application, microsoft included, that are GUI applications that when it runs an update, it closes the current application and opens an updater screen, then when the update is finished, it closes the update window and reopens the applicaiton just doing it with console instead of a GUI app
jcotton42
jcotton4215mo ago
huh? I'm talking about like I already have a terminal open, and run main_app.exe, or whatever you shouldn't be closing my terminal also, that happens with GUI apps b/c the app that's drawing the window has exited for a terminal app the app drawing the window is... the terminal it's not comparable
arion
arion15mo ago
Whats wrong with just doing
Process.Start("test.exe");
Environment.Exit();
Process.Start("test.exe");
Environment.Exit();
jcotton42
jcotton4215mo ago
your terminal app just should not care there's no reason for it to do so
Mekasu0124
Mekasu0124OP15mo ago
I have this currently, but it doesn't close the window
I'm talking about like I already have a terminal open, and run main_app.exe, or whatever you shouldn't be closing my terminal.
this is like telling the GUI application that it shouldn't be closing your main applications window because you already have the main applications window open....
arion
arion15mo ago
What jcotton is likely trying to say is that your program shouldnt need to know who the parent who executed it is. It opens a lot of potential issues up Console applications arnt windows like a gui app
jcotton42
jcotton4215mo ago
this is like telling the GUI application that it shouldn't be closing your main applications window because you already have the main applications window open....
no it's not the window here is being drawn and maintained by the terminal application not your application it's literally none of your app's business what my terminal window setup is like you're a console app, not a gui app
arion
arion15mo ago
For example on this, the discord server has a bot called modix which allows for outputting the terminal contents, in a way, its similar
Mekasu0124
Mekasu0124OP15mo ago
ok so instead of getting passive aggresive responses, would you be inclined to elaborate on what potential issues this could cause by doing what I'm wanting to do with a console based application please?
jcotton42
jcotton4215mo ago
there's just no reason to do it like that
Mekasu0124
Mekasu0124OP15mo ago
that's not an explanation of what potential issues it could cause...
arion
arion15mo ago
Windows Terminal: if you close the main window it either minimizes to tray or closes all terminal instances active in it
Mekasu0124
Mekasu0124OP15mo ago
ok that I can understand, so what I'm wanting to do would involve opening my second application in a new terminal instance instead of the current terminal instance so other than the lack of potential issues that it could cause with what I'm wanting to do, I could do an alternative of just clearing the console before the second process starts, and then clearing the console when the second process is finished, yes? so that I get the window closing effect without actually closing the windows
arion
arion15mo ago
Console.Clear() doesnt close the terminal instance, it just removes all foreground and background text and formatting. It erases all output from the instance A "new terminal instance" is dependent on the terminal application Windows terminal creates new tabs for each "terminal" instance the old conhost or whatever its called used to have independent instances, but that brings us back to what jcotton said implementation of that is dependent on the terminal application
Mekasu0124
Mekasu0124OP15mo ago
ok that makes sense so far
arion
arion15mo ago
conhost was that version's "Terminal Application", the latest (Windows Terminal) is the current "Terminal Application" on mac its something different and on linux too
Mekasu0124
Mekasu0124OP15mo ago
right. so what would be the best way to go about getting the window close effect without causing any potential harm or problems?
arion
arion15mo ago
If you use VS Code or Rider they have their own integrated terminals, in those cases, the terminal application would be their own respective processes VS Code would be its own Terminal application
jcotton42
jcotton4215mo ago
again, why do you want that effect so bad?
Mekasu0124
Mekasu0124OP15mo ago
I use visual studio, and I publish my projects to .exe's and test from those so that it opens in a legit windows terminal
arion
arion15mo ago
Specifically check for that specific terminal application and exit your program immediately if its not supported Potential harm would be: you kill unintended processes
Mekasu0124
Mekasu0124OP15mo ago
because I like that effect. Is that such a bad thing? At this point I'm aware of your opinion that "it's literally none of your app's business what my terminal window setup is like", and that it can cause potential problems (which is currently being discussed), so I'm asking of a nicer way to go about doing what I want to achieve because it's what i want to do with my application. so given that the potential harm would be killing unintended processes, I'd rather not do that. I'd rather find a cleaner or nicer way to go about it, even if it is just using Console.Clear() to clear the screen in between processes starting/stopping
arion
arion15mo ago
What happens when I start a console program .exe If you close the Terminal window if its being ran within VS Code it would close VS Code
arion
arion15mo ago
What happens whenn I start a console program in the old conhost
Mekasu0124
Mekasu0124OP15mo ago
ok. So then I'll just clear the console before/after each new process. no problems there
jcotton42
jcotton4215mo ago
is this the kind of app someone would start from their terminal?
Mekasu0124
Mekasu0124OP15mo ago
nope it's an exe file it's a console based application that is packaged down into an exe file that the user would click or double click to open and run that opens in a terminal
arion
arion15mo ago
You dont necessarily need to create a new terminal instance tbf, you could start your 2nd program and read that info to your first program eg.
Console.Clear();
StartOurChildProgramAndListenToOutput();
Console.Clear();
StartOurChildProgramAndListenToOutput();
though it would likely be at the point where there's little use to start a new program since you can already do that in normal code:
Console.WriteLine("Hello there");
await Task.Delay(TimeSpan.FromSeconds(2));
Console.Clear();
Console.WriteLine("Welcome back!");
Console.WriteLine("Hello there");
await Task.Delay(TimeSpan.FromSeconds(2));
Console.Clear();
Console.WriteLine("Welcome back!");
Mekasu0124
Mekasu0124OP15mo ago
well here's a good question for you using that code snippet what's the difference between
Console.WriteLine("Hello There");
await Task.Delay(TimeSpan.FromSeconds(2));
Console.Clear();
Console.WriteLine("Welcome Back!");
Console.WriteLine("Hello There");
await Task.Delay(TimeSpan.FromSeconds(2));
Console.Clear();
Console.WriteLine("Welcome Back!");
and the code
Console.WriteLine("Hi");
Thread.Sleep(2000);
Console.Clear();
Console.WriteLine("WB!");
Console.WriteLine("Hi");
Thread.Sleep(2000);
Console.Clear();
Console.WriteLine("WB!");
?
arion
arion15mo ago
One freezes the entire console application and the other doesnt
Mekasu0124
Mekasu0124OP15mo ago
oh ok. cool
arion
arion15mo ago
You cant move your window during that time
Mekasu0124
Mekasu0124OP15mo ago
so with starting a new process, which one would be more beneficial?
jcotton42
jcotton4215mo ago
that's a thing for GUI apps, not console ones
arion
arion15mo ago
Just make it a new method called "DisplayPage2" or something
public static async Task Main(string[] args)
{
Console.WriteLine("Welcome to my app");
await DisplayPage2();
}

private static async Task DisplayPage2()
{
Console.Clear();
Console.WriteLine("Page 2: My Woes");
}
public static async Task Main(string[] args)
{
Console.WriteLine("Welcome to my app");
await DisplayPage2();
}

private static async Task DisplayPage2()
{
Console.Clear();
Console.WriteLine("Page 2: My Woes");
}
ah whoops, yea you right you could do page transitions without needing to start a new process
arion
arion15mo ago
eg. this
arion
arion15mo ago
Thats just this
Console.Clear();
Console.WriteLine("Page 1");
await Task.Delay(TimeSpan.FromSeconds(2));
Console.CursorVisible = false;
for (var i = 0; i < Console.BufferHeight; i++)
{
Console.Clear();
Console.CursorTop = i;
Console.WriteLine(new string('#', Console.BufferWidth));
await Task.Delay(10);
}
Console.CursorVisible = true;
Console.Clear();
Console.WriteLine("Page 2");
Console.Clear();
Console.WriteLine("Page 1");
await Task.Delay(TimeSpan.FromSeconds(2));
Console.CursorVisible = false;
for (var i = 0; i < Console.BufferHeight; i++)
{
Console.Clear();
Console.CursorTop = i;
Console.WriteLine(new string('#', Console.BufferWidth));
await Task.Delay(10);
}
Console.CursorVisible = true;
Console.Clear();
Console.WriteLine("Page 2");
Also just saying, there's an entire nuget package dedicated to making the console do neat tricks its called Spectre.Console
arion
arion15mo ago
The documentation is found here
Spectre.Console - Welcome!
Spectre.Console is a .NET library that makes it easier to create beautiful console applications.
Mekasu0124
Mekasu0124OP15mo ago
this is pretty neat would you mind if I sent you a dm?
arion
arion15mo ago
I'm actually about to head to bed, i'll read dms when i wake up
Mekasu0124
Mekasu0124OP15mo ago
ok no problem. thank you for your help
Want results from more Discord servers?
Add your server