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);

        }           
    } 
CharlesB
  • 86,532
  • 28
  • 194
  • 218
ale
  • 3,301
  • 10
  • 40
  • 48

7 Answers7

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.

KeithS
  • 70,210
  • 21
  • 112
  • 164
1

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

George Duckett
  • 31,770
  • 9
  • 95
  • 162
1

Take the stopwatch variable declaration outside of the method.

Thomas Li
  • 3,358
  • 18
  • 14
  • what type of variable is this? or how can I write the variable outside? ... I was thinking some similiar.. – ale May 05 '11 at 16:01
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
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
0

What about using:

var startTime = DateTime.Now;

... your code

var elapsed = DateTime.Now - startTime;
DanielB
  • 19,910
  • 2
  • 44
  • 50
  • 4
    DateTime is [not a good method](http://stackoverflow.com/questions/2923283/stopwatch-vs-using-system-datetime-now-for-timing-events/2923349#2923349) of calculating elapsed time. – Brad Christie May 05 '11 at 15:59
  • @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. – DanielB May 05 '11 at 16:06
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 !.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
-1

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

Pravin
  • 1,059
  • 8
  • 17