6

Usually, calling windows application in command prompt does not wait its exit. Once I type myFormApp.exe and press enter, a form will display and command prompt moves to next line immediately.

C:\> myFormApp.exe
C:\>     # this line will display immediately

I know using cmd /c will wait for application exit.

C:\> cmd /c myFormApp.exe
C:\>     # this line will display after myFormApp.exe closed

But I want to waiting even in lack of cmd /c.

Is there any way to prevent command prompt to move to new line, with editing myFormApp.exe source code?

Adding AllocConsole to windows application will show console, but it does not make command prompt to wait exiting.

if (args.Length > 0)
{
    // Command line given, display console
    AllocConsole();
}
else
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

Any advices will be appreciated. Thanks!

2 Answers2

3

No need to write additional app. Such one already exists in the Windows - start with /wait option. It won't just wait for your application to finish execution, it will also set the returned errorlevel (aka ExitCode).

2

You could write a simple console application that wraps your winforms application. You could either launch a process inside the console application:

static void Main(string[] args)
{
    Console.WriteLine("Hello!");

    var process = new Process() { StartInfo = new ProcessStartInfo { FileName = @"WindowsFormsApplication1.exe" } };
    process.Start();
    process.WaitForExit();

    Console.WriteLine("bye bye!");
}

Alternatively you could add reference to your winforms application and instantiate the winforms class inside your console application:

static void Main(string[] args)
{
    Console.WriteLine("Hello!");

    var mainForm = new WindowsFormsApplication1.Form1();
    mainForm.ShowDialog();

    Console.WriteLine("bye bye!");
}
Bartosz
  • 732
  • 9
  • 30
  • 1
    When executing the wrapper console application by double click, a command prompt will display. And this is not desired behavior. Is there any way to avoid it? –  Mar 13 '14 at 11:02
  • No, not really. You could change the output type of the wrapper application to Windows Application which would make the window not appear, but then again if you launch it from console it will not wait for the execution to finish. Not really sure what you're trying to achieve here, probably to fix the wrong problem. Blocking the prompt to move to a new line doesn't fix anything. A user can always open a new prompt and do whatever. – Bartosz Mar 13 '14 at 11:20
  • You can use interop to hide and show the console window. Hide it on start up, show on exit. This way if you double click your app in windows, no console will show, if you launch it from an existing console, it will hide for the time of winforms app execution but then show back again when you exit. http://stackoverflow.com/questions/3571627/show-hide-the-console-window-of-a-c-sharp-console-application I wound't advise doing that though. – Bartosz Mar 13 '14 at 11:24
  • What I want to do is to wait for the execution of an windows application, that is called from command line. And the app will now show the console when exe is double clicked. –  Mar 14 '14 at 01:15
  • 6
    There is no way to build an application that both (a) causes the command prompt to wait when launched from the command prompt and (b) doesn't create a new console window when launched from Explorer. These are mutually exclusive because they are both based on the exact same thing: whether the executable is marked as a console application or a GUI application. The only solution is to have two different executables, though of course the actual code can be shared via a DLL or one executable can launch the other. – Harry Johnston Mar 14 '14 at 01:31