如何在我的流程中使用性能计数器? C#

发布于 2024-11-01 22:55:25 字数 410 浏览 4 评论 0原文

我有一个应用程序,我需要在结束时显示该过程的总时间。

我正在使用 Swith 案例,

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; 

因此,当进程结束时,我需要显示消息和类似这样的内容:“进程在 30 秒内完成”...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停滞 2024-11-08 22:55:25

您应该使用 Stopwatch 类:

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

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);
彩扇题诗 2024-11-08 22:55:25

您不需要性能计数器。

您可以使用秒表

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

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);
绮烟 2024-11-08 22:55:25

您不需要性能计数器即可将此信息放在控制台上:使用 秒表

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文