У меня есть следующий код в точке входа WPF
public partial class App : Application
{
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;
protected override void OnStartup(StartupEventArgs e)
{
if (e.Args.Length == 0)
{
// Launch GUI and pass arguments in case you want to use them.
base.OnStartup(e);
new MainWindow().ShowDialog();
}
else
{
//Do command line stuff
if (e.Args.Contains("-i"))
{
// redirect console output to parent process;
// must be before any calls to Console.WriteLine()
AttachConsole(ATTACH_PARENT_PROCESS);
var stopWatch = new Stopwatch();
stopWatch.Reset();
stopWatch.Start();
for (int i = 0; i < 100; i++)
{
Console.WriteLine(i);
}
stopWatch.Stop();
var ts = stopWatch.Elapsed;
var elapsedTime = $"{ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds / 10:00}";
Console.WriteLine("RunTime " + elapsedTime);
}
else
{
AttachConsole(ATTACH_PARENT_PROCESS);
Console.WriteLine("Incorrect arguments");
}
}
Shutdown();
}
}
Приглашение немедленно возвращается, как будто мое приложение работает в фоновом режиме.
![enter image description here](https://i.stack.imgur.com/ScuGe.png)
Я ожидаю, что приложение заблокирует возврат консоли до завершения процесса.