My program runs a batch file in cmd.exe, after it finished I want to display a MessageBox to user saying Finished in #.## seconds
,
I'm redirecting CMD output to a textbox using process.BeginOutputReadLine()
, this is the code I tried:
if (e.Data == null)
{
string time = process.TotalProcessorTime.Seconds.ToString();
MessageBox.Show("Finished in " + time + " seconds");
}
It took about 7-15 seconds to complete the process, but the MessageBox displayed Finished in 0 seconds
.
How do I get the accurate time it took to complete in seconds?
The difference between the end time and start time is the execution time. Get the execution time by subtracting the start time from the end time.
%%time is a magic command. It's a part of IPython. %%time prints the wall time for the entire cell whereas %time gives you the time for first line only. Using %%time or %time prints 2 values: CPU Times.
In order to calculate the time elapsed in executing a code, the time module can be used. Save the timestamp at the beginning of the code start using time() . Save the timestamp at the end of the code end . Find the difference between the end and start, which gives the execution time.
Stopwatch watch = new Stopwatch();
watch.Start();
//Do things
watch.Stop();
Text = watch.Elapsed.TotalSeconds.ToString();
Have you tried process.ExitTime.Subtract(process.StartTime).TotalSeconds
?
Edited to add: Note that you will get an exception if you try to use this before the process has exited. Per the documentation for ExitTime
, use the HasExited
property if you have any doubt as to whether this is the case or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With