如何使一个没有窗户/命令行应用程序回报,但继续执行的背景是什么?命令行、应用程序、窗户、背景

2023-09-03 22:45:39 作者:划船不靠浆全靠浪

我在使用.NET编写一个命令行应用程序。该应用程序本身是相当简单的,但它必须同步连接到Web服务,这反过来又连接到Oracle数据库,和的的作品是喜欢采取他们的时间。

I'm writing a command-line application in .Net. The app itself is fairly simple, but it has to connect synchronously to a web-service, which in turn has to connect to a Oracle database, and those pieces are fond of taking their time.

有没有一种简单的方法(没有将我的应用程序的EXE二)继续执行,但仍然得到执行命令提示符?

Is there a straightforward way (without dividing my app exe in two) to continue executing but nonetheless yield execution to the command prompt?

这是Windows系统,因此没有&放大器;。此外,我不能使用的cmd.exe的开始cmdlet的。

It's Windows, so no "&". Also, I cannot use cmd.exe's "start" cmdlet.

推荐答案

我不相信这是可能不会从你的应用程序中运行一个后台进程。然而,一个相当干净的方法,这样做可能会修改你的主要方法,像这样:

I don't believe it's possible without running a background process from your application. However, a fairly clean way to do so might be to modify your Main method like so:

static void Main(string[] args)
{
    if (args.Length > 0 && args[0] == "run")
    {
        //actually run your application here
    }
    else
    {
        //create another instance of this process
        ProcessStartInfo info = new ProcessStartInfo();
        info.FileName = Assembly.GetExecutingAssembly().Location;
        info.Arguments = "run";
        info.UseShellExecute = false;
        info.CreateNoWindow = true;

        Process.Start(info);
    }
}

类似的东西反正只是写这关我的头顶。基本上,它创建相同的可执行文件的新实例,但新工艺看到运行命令行参数和做的工作,而不是生成一个新的实例。设置我的选择应该允许的衍生进程打印到现有的控制台也。

Something like that anyway, just writing this off the top of my head. Basically, it creates a new instance of the same executable, but the new process sees the "run" command line argument and does the work rather than spawning a new instance. Setting the options I have "should" allow the spawned process to print to the existing Console as well.