The example shows how the code works, but it is necessary that the response comes not by the next command, but by continuing the request.

For example, we write C:\app.exe /h /s /e and in the same line we get the answer:

enter image description here

The problem is that the answer comes with a new command:

enter image description here

Code in Program.cs (Windows Form application):

 using System; using System.Linq; using System.Windows.Forms; using System.Runtime.InteropServices; using System.IO; static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var consoleWriter = new GUIConsoleWriter(); var form = new Form1(); bool needRun = true; foreach (string arg in args) { switch (arg) { case "/h": form.Check1(); Console.WriteLine("\r\n" + "Команда /h выполнена"); needRun = false; break; case "/s": form.Check2(); Console.WriteLine("\r\n" + "Команда /s выполнена"); needRun = false; break; case "/e": form.Check3(); Console.WriteLine("\r\n" + "Команда /e выполнена"); needRun = false; break; } } if (needRun) { Application.Run(form); } } public class GUIConsoleWriter { private const int ATTACH_PARENT_PROCESS = -1; StreamWriter ConsoleWriter; public GUIConsoleWriter() { var stdout = Console.OpenStandardOutput(); ConsoleWriter = new StreamWriter(stdout); ConsoleWriter.AutoFlush = true; AttachConsole(ATTACH_PARENT_PROCESS); } [DllImport("kernel32.dll")] private static extern bool AttachConsole(int dwProcessId); } 
  • @Igor if you remove "\ r \ n", then it will remove only the new line, it will be registered as a new command (that is, a new request appears in the form of C:\> and the text is in it), but the first command should not be completed until the answer appears as text. - Vitokhv
  • one
    I suspect that it is a matter of creating GUIConsoleWriter, try not to create it, because there is no need to join the console output again when it already has the console. - Daniel Protopopov
  • @DanielProtopopov I found a simplification to the code, but it still displays the answer with a new command, through C:\> attach the code with the answer. - Vitokhv
  • Answer me please, otherwise I do not catch up - why do you need to call AttachConsole in the CONSOLE program? - Daniel Protopopov
  • one
    I think the problem here is that when launching the WinForms program from the console, it is created and disconnected from the console itself because it was just started from it (just as if it was done from windows explorer). And after that you connect to this console as the root application, which is why it turns out this way. Here, most likely, everything is correct, and I don’t think that it is possible to get around this somehow except for preventing the disconnection from the console, although it will be very dreary and labor-intensive. - Daniel Protopopov

1 answer 1

Simplified code, but the result is the same (the answer comes with a command):

 using System; using System.Linq; using System.Windows.Forms; using System.Runtime.InteropServices; static class Program { [DllImport("kernel32.dll")] private static extern bool AttachConsole(int procid); [STAThread] static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var form = new Form1(); if (AttachConsole(-1)) { foreach (string arg in args) { switch (arg) { case "/h": form.Check1(); Console.Write(Environment.NewLine + "Команда /h выполнена"); break; case "/s": form.Check2(); Console.Write(Environment.NewLine + "Команда /s выполнена"); break; case "/e": form.Check3(); Console.Write(Environment.NewLine + "Команда /e выполнена"); break; } } } else { Application.Run(form); } } }