Process.Start 和分配的资源 [英] Process.Start and allocated resources

查看:64
本文介绍了Process.Start 和分配的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

Process pLight = new Process();
pLight.StartInfo.UseShellExecute = false;
pLight.StartInfo.FileName = "MyCommand.exe";
//
pLight.StartInfo.Arguments = "-myparam 0";
pLight.Start();
//
pLight.StartInfo.Arguments = "-myparam 1";
pLight.Start();
//
pLight.StartInfo.Arguments = "-myparam 2";
pLight.Start();

问题是:每次调用 Start() 时都会创建"一个新进程?

The question is: a new process is "created" each time i invoke Start()?

来自 Process.Start 文档:

如果进程资源启动则返回真;如果没有启动新进程资源(例如,如果重用现有进程),则为 false.

Returns true if a process resource is started; false if no new process resource is started (for example, if an existing process is reused).

但每次我调用这个方法时,我都会得到true.那么我怎样才能重用相同的过程呢?有没有办法使用同一个进程运行多个命令?

but each time i invoke this method i get true. So how can I reuse the same process? Is there a way to run multiple commands using the same process?

推荐答案

pLightStartInfo = new ProcessStartInfo();
pLightStartInfo.UseShellExecute = false;
pLightStartInfo.FileName = "MyCommand.exe";
pLightStartInfo.Arguments = "-myparam 0";
pLightStart();
pLightStartInfo.Arguments = "-myparam 1";
pLightStart();
pLightStartInfo.Arguments = "-myparam 2";

Process pLight = new Process(pLightStartInfo); // first time so a new Process will be started

Process myOtherProcess = Process.Start(pLightStartInfo); // second time so myOtherProcess would reuse pLight, given original hadn't closed so both would be "pointing" at one MyCommand.exe

我自己从来没有做过,但这似乎就是这个意思.

Never done it myself but that's what it appears to mean.

这篇关于Process.Start 和分配的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆