开发者

How to make a windowless / command-line application return but continue executing in background?

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.

Is there a straightforward way (without dividing my app exe in two) to continue executing but nonetheless yield executi开发者_运维百科on to the command prompt?

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.


Your app could launch a second instance of itself, with an additional parameter. So if your run "MyApp param1 param2", then it launches "MyApp -synch param1 param2" and terminates. The second instance sees the "-synch" parameter and does the work.

As a slight alternative, running "MyApp p1 p2" actually does the work (synchronously), but if you run "MyApp -asynch p1 p2" then it launches "MyApp p1 p2" and terminates.

It is two processes, but at least it's only one exe


No, I don't believe this is possible given your constraints. Your options are either to retain control, or (as you mention) to divide your application into two such that the initial executable is just a shim that instantiates the main program and then returns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜