0

I need a method that give me the time elapsed awhile my process. I call it at start the process and call it again at finish the process, and the method print the total time elapsed.

This is my method, but always print the time in 00:00. Why is happening this??

public void GetTimeElapsed(string filePath, int logSelected, bool time, IUserOptions userOptions) { var stopwatch = new System.Diagnostics.Stopwatch(); LogBinaryWriter BinaryWriter = new LogBinaryWriter(); string timeElapsed = ""; if(time == true) { stopwatch.Start(); } if (time == false) { stopwatch.Stop(); TimeSpan timeSpan = stopwatch.Elapsed; timeElapsed = (string.Format("\nFile Generated: {0}\nTime Elapsed: {1} minute(s) {2} second(s)", BinaryWriter.CreateLogFileName(filePath, Convert.ToInt32(logSelected)), timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10 + "\n")); userOptions.DisplayUserMessage(timeElapsed); } } 

7 Answers 7

2

Look where you're declaring stopwatch; it's a local variable. That means you're creating and using two different stopwatches; the first one is started when you call the method with a "true" parameter, then disposed of when the method ends and the variable goes out of scope. The second is declared, never started, then its time examined and logged.

To solve the problem, declare an instance variable ("field") for the Stopwatch. That will keep it in scope as long as the object is around, meaning it will keep running after the method ends, and will still be the same instance when you come back to it to stop and examine it.

Sign up to request clarification or add additional context in comments.

Comments

1

Your stopwatch variable is local. When you call the function a second time, it's initialized again.

You need to move the declaration up to class level.

System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); public void GetTimeElapsed(string filePath, int logSelected, bool time, IUserOptions userOptions) { ... etc 

Comments

1

You're creating a new stopwatch each time this method is called, but it looks like that should be persistent between method calls.

Comments

1

Take the stopwatch variable declaration outside of the method.

1 Comment

what type of variable is this? or how can I write the variable outside? ... I was thinking some similiar..
0

What about using:

var startTime = DateTime.Now; ... your code var elapsed = DateTime.Now - startTime; 

2 Comments

DateTime is not a good method of calculating elapsed time.
@Brad Christie: Thanks for the link. Good point. In this case it seems the OP will use the elapsed time only for user information and for that it may be precise enough. But again thanks for pointing this out.
0
 if(time == true) { stopwatch.Start(); } if (time == false) { stopwatch.Stop(); ... } 

If time is true, you only ever start the stopwatch.

If it is false, you never start it.

A better structure would be:

if(time) { stopwatch.Start(); } ... //code to measure here if (time) { stopwatch.Stop(); // log elapsed time } 

Note:

If you have a boolean type, you don't compare it to true or false. Just use it directly and if you want to invert it just use !.

Comments

-1

You need to use timeSpan.TotalMinutes instead timestamp.Minutes. Refer timespan documentation

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.