我希望能够像 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/