开发者

Mimicking batch file with C# with constraint

I have asked the question Mimic batch file with C#, and got some good answers.

But,开发者_如何学C the problem is that these three commands should be run in the same process.

vsperfcmd /start:coverage /output:run.coverage
hello
vsperfcmd /shutdown

So, the Process.Start method

Process.Start("vsperfcmd", "/start:coverage /output:run.coverage");
Process.Start("hello");
Process.Start("vsperfcmd", "/shutdown");

shouldn't work, as it runs three processes independently.

Is there any other way to run those three commands in the same process? I mean, run one after another.


If you don't actually necessarily need the steps to be run in the same process, but just sequentially, you can use the 'WaitForExit' method on Process, which will block execution on the current thread until the process completes. For example, the following code will open notepad, wait for you to close it (process exits) and then open a second instance of notepad:

        p.StartInfo.FileName = "notepad.exe";
        p.Start();
        p.WaitForExit();
        p.Close();

        p.StartInfo.FileName = "notepad.exe";
        p.Start();
        p.WaitForExit();
        p.Close();

However, the two instances will occur in different processes - you can see this by checking the PID of notepad.exe in Task Manager, for example.

If you really want to run several CLI programs in sequence in the same process, you may want to simply open a process running 'cmd.exe' and feed commands to it via its StandardInput. The StandardInput stream is the stream that the program reads its input from - in the case of cmd.exe, it's exactly as if you'd typed those commands into it directly. For example, try this:

        Process p = new Process();

        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardInput = true;

        p.Start();
        p.StandardInput.WriteLine("echo Hello there!");
        p.StandardInput.WriteLine("dir");
        p.StandardInput.WriteLine("echo Goodbye!");

        p.WaitForExit();
        p.Close();

One problem with this approach is that you don't know whether the last command you passed in has actually finished executing that easily... the 'WaitForExit' call will wait until the process exits of its own accord, but in the case of the cmd.exe window that would require a user to come along and click the 'X' in the corner. You could skip the 'WaitForExit' call, but that may terminate the process while your last command is still actually doing something.

Luckily in this particular case, cmd.exe will terminate itself if you feed it the 'exit' command:

        Process p = new Process();

        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardInput = true;

        p.Start();
        p.StandardInput.WriteLine("echo Hello there!");
        p.StandardInput.WriteLine("dir");
        p.StandardInput.WriteLine("echo Goodbye!");
        p.StandardInput.WriteLine("exit");

        p.WaitForExit();
        p.Close();

As it goes, you have to set the UseShellExecute and RedirectStandardInput properties to true and false respectively, or using the StandardInput stream will throw an exception of one type or another - try it and see! You can read more about it here:

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardinput.aspx


Looks like you could use System.Diagnostics.Process()

var proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.FileName="vsperfcmd";
proc.StartInfo.Arguments="/start:coverage /output:run.coverage";
proc.Start();
proc.WaitForExit();

var proc2 = new System.Diagnostics.Process();
proc2.EnableRaisingEvents=false;
proc2.StartInfo.FileName="hello";
proc2.StartInfo.Arguments="";
proc2.Start();
proc2.WaitForExit();

var proc3 = new System.Diagnostics.Process();
proc3.EnableRaisingEvents=false;
proc3.StartInfo.FileName="vsperfcmd";
proc3.StartInfo.Arguments="/shutdown";
proc3.Start();
proc3.WaitForExit();


if running the batch file directly does not fit your needs, I believe you can open the console from c# and send your command to it, once you have grabbed the standard input of the console.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜