开发者

How make time elapsed method?

开发者 https://www.devze.com 2023-03-04 09:26 出处:网络
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.

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 E开发者_运维知识库lapsed: {1} minute(s) {2} second(s)",
            BinaryWriter.CreateLogFileName(filePath, Convert.ToInt32(logSelected)),
            timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10 + "\n"));
            userOptions.DisplayUserMessage(timeElapsed);

        }           
    } 


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.


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


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


Take the stopwatch variable declaration outside of the method.


What about using:

var startTime = DateTime.Now;

... your code

var elapsed = DateTime.Now - startTime;


    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 !.


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

0

精彩评论

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

关注公众号