C# 获取自应用程序启动以来经过的时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10161088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:41:53  来源:igfitidea点击:

Get elapsed time since application start

c#.netwpftimer

提问by Michael IV

I am porting an app from ActionScript3.0 (Flex) to C# (WPF).AS3.0 has got a handy utility called getTimer()which returns time since Flash Virtual machine start in milliseconds.I was searching in C# through classes as

我正在将应用程序从 ActionScript3.0 (Flex) 移植到 C# (WPF)。AS3.0 有一个名为getTimer()的方便实用程序,它以毫秒为单位返回自 Flash 虚拟机启动以来的时间。我通过类在 C# 中搜索

DateTime
DispatcherTimer
System.Diagnostics.Process
System.Diagnostics.Stopwatch

but found nothing like this.It seems a very basic feature to me.For example Unity3D which runs on Mono has something familiar. Do I miss some utility here?

但没有发现这样的东西。对我来说,这似乎是一个非常基本的功能。例如,在 Mono 上运行的 Unity3D 有一些熟悉的东西。我在这里错过了一些实用程序吗?

Thanks in advance.

提前致谢。

采纳答案by spender

Process.GetCurrentProcess().StartTimeis your friend.

Process.GetCurrentProcess().StartTime是你的朋友。

..so to get elapsed time since start:

..so 获取自开始以来经过的时间:

DateTime.UtcNow - Process.GetCurrentProcess().StartTime.ToUniversalTime()

alternatively, if you need more definition, System.Diagnostics.Stopwatchmight be preferable. If so, start a stopwatch when your app starts:

或者,如果您需要更多定义,System.Diagnostics.Stopwatch可能更可取。如果是这样,请在您的应用程序启动时启动秒表:

Stopwatch sw = Stopwatch.StartNew();

then query the sw.Elapsedproperty during your execution run.

然后sw.Elapsed在执行运行期间查询该属性。