简体   繁体   English

C#System.Diagnostics.Process:确定C ++编译过程花费了多少时间

[英]C# System.Diagnostics.Process: determine how much time C++ compilation process took time

 Log("Starting to compile...\n");
        Process p = new Process();
        p.StartInfo.FileName = CompilationCommand;
        p.StartInfo.Arguments = SourceFileName;
        p.Start();
        Log("Compiling finished in " + (p.StartTime - p.ExitTime).Seconds + " seconds\n");

This code doesn't work. 此代码无效。 How can I find out how much time was spent on compiling a C++ source file with g++ compiler? 我如何找出用g ++编译器编译C ++源文件花了多少时间?

You can use the WaitForExit function in process to wait for the process to exit along with the Stopwatch class to measure the time it took. 您可以在进程中使用WaitForExit函数来等待进程与Stopwatch类一起退出,以测量花费的时间。 This would result in you having a thread waiting for the process to exit. 这将导致您有一个线程等待进程退出。

Stopwatch watch = Stopwatch.StartNew();
// start process as before
process.WaitForExit();
TimeSpan ts = watch.Elapsed;

Processes run asynchronously to other processes but you don't wait for it to finish. 进程与其他进程异步运行,但是您不必等待它完成。 By the time your Log call returns, the process could still be running away. 在您的Log调用返回时,该过程可能仍在运行。

Have a look at implementing the process.Exited event. 看一下实现process.Exited事件。

This MSDN article has a good sample: 此MSDN文章有一个很好的示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM