C# hybird Winforms / console app returns to cmd prompt
<p>I have seen many examples of a hybrid gui/cli app for C#. I have implemented such an app, but I am having a hard time figuring out how to prevent the .exe, when run on the command line, from not returning back to the prompt right away.</p>

<pre><code>    //defines for commandline output     
    [DllImport("kernel32.dll")]
    static extern bool AttachConsole(int dwProcessId);
    private const int ATTACH_PARENT_PROCESS = -1;

    [STAThreadAttribute]
    static void Main(string[] args)
    {
        // load cli
        // redirect console output to parent process;         
        // must be before any calls to Console.WriteLine()         
        AttachConsole(ATTACH_PARENT_PROCESS);

        if (args.Length == 0)
        {
            //loads gui
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new form_Main());
        }
        else
        {

            cli_main cli = new cli_main();
            cli.start_cli(args);

            Console.WriteLine("finished");

            System.Windows.Forms.SendKeys.SendWait("{ENTER}");
            Application.Exit();

        }
    }
</code></pre>

<p>I get the following output</p>

<pre><code>C:\Users\john\Documents\Visual Studio 2010\Projects\test\test\test\bin\Debug&gt;test.exe -param1 test 
-param2 test2

C:\Users\john\Documents\Visual Studio 2010\Projects\test\test\test\bin\Debug&gt;Output was successful. File saved as: c:\test\test.html
finished
</code></pre>

<p>The line "finished" is a string I output when I know I have reach end of my main code... this works fine in Winforms, my project is Winforms and I started it as a gui but now I am trying to make it hybrid gui/cli</p>

<p>And it seems to be running my main code and threads I see them in debugging and it creates my final output file...</p>

<p>I just am puzzled as to how to keep the .exe when executed from cmd line with it's parameters, to not return to the command prompt?? And have it wait with a blinking cursor, then output the line about the html file and then the line "finished", then finally go back to the command prompt. </p>

<p>I've tried numerous things like removing</p>

<pre><code>System.Windows.Forms.SendKeys.SendWait("{ENTER}");
Application.Exit();
</code></pre>

<p>and instead of using <code>Application.Exit();</code> use <code>Environment.Exit(0);</code> but it always returns to the command prompt right away, also I tried putting in sleep for 5 secs after the line</p>

<pre><code>cli.start_cli(args);
</code></pre>

<p>but that didn't work either, I guess I don't understand how it can return to command prompt right away and it hasn't even it the line</p>

<pre><code>Console.WriteLine("finished");
</code></pre>
