ale
ale

Reputation: 3431

How can I use a perfomance counter for my process? C#

I have an application , and I need to show the total time of the process when this ends.

I am using the Swith case,

case "4":
  Console.WriteLine("Generating file..." + "\n");
 _loader.GenerateBinaryLog(filePath, logSelected);
 Console.WriteLine("File Generated: " + _writer.getBinaryFileName(filePath, Convert.ToInt32(logSelected)) + "\n");
 logSelected = "-1";
 Console.ReadKey();
 break; 

So, when the process ends I need to show the message and something like this: "process finished in 30 seconds"...

Upvotes: 1

Views: 235

Answers (3)

Cheeso
Cheeso

Reputation: 192657

You don't need a performance counter.

You can use a Stopwatch,

      var stopwatch = new System.Diagnostics.Stopwatch();
      stopwatch.Start();
      ... do work here
      stopwatch.Stop();
      TimeSpan ts = stopwatch.Elapsed;
      Console.WriteLine("  Elapsed {0:N2}", ts.TotalSeconds);

Upvotes: 0

Felice Pollano
Felice Pollano

Reputation: 33272

You don't need a performance counter to just put this info on the console: use a Stopwatch.

Upvotes: 0

SLaks
SLaks

Reputation: 888203

You should use the Stopwatch class:

var sw = new Stopwatch();
sw.Start();
//Do things...
sw.Stop();
Console.WriteLine("Operation took {0:#,0.0} seconds", sw.Elapsed.TotalSeconds);

Upvotes: 1

Related Questions