C# 关闭标准输出

标签 c# console redirectstandardoutput

我希望能够像 wget -b 一样从控制台分离我的程序。代码片段可能看起来像

static void Main(string[] args)
{
    var settings = new Settings(args);
    if (settings.Background)
    {
        /*Tell the user what's going on.*/
        System.Console.WriteLine("Detatching from console. The program will still be running.");
        System.Console.Out.Close();
    }
    /*do work then exit.*/
}

但是 System.Console.Out.Close(); 没有做正确的事情。

澄清一下,“正确的事情”是,当从控制台运行这个程序时,提示应该重新出现。或者,当从 explorer.exe 运行此程序时,控制台窗口应该关闭。

如果我不清楚,请告诉我。

最佳答案

您引用的wget 的源代码可用here .您会注意到在 mswindows.c 的第 193 - 314 行中,实现了一个 fork 过程。它们产生了一个新的 wget 实例,并向它传递相同的参数。

评论也很丰富:

Windows doesn't support the fork() call; so we fake it by invoking another copy of Wget with the same arguments with which we were invoked.

在第 102 行:

Under Windows 9x, if we were launched from a 16-bit process ... the parent process should resume right away. Under NT ... this is a futile gesture as the parent will wait for us to terminate before resuming.

简短的回答似乎是“不要那样做”。

关于C# 关闭标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9913120/

相关文章:

c# - 如何进行这种转换?

c# - 使用 asp.net core 2.2 恢复 nuget 包时出现问题

ocaml - 重定向标准输出 OCaml

c# - 重定向 N 级子进程的标准输出

c# - 为什么 Visual Studio 2010 不允许在 linq 查询中使用 "is null"而 VS2017 允许?

c# - 使用 Autofac 将 SignalR IHubContext 注入(inject) Controller

c++ - Linux c++ 控制台获取关键状态

c# - 抑制控制台从导入的 DLL 打印

python - 使用调试控制台时出错 : DeprecationWarning: The `use_readline` parameter is deprecated and ignored since IPython 6. 0

python - 将输出重定向到文件时(使用 os.system 时)奇怪的 python 行为