获取应用程序启动后经过的时间

Mic*_* IV 17 .net c# wpf timer

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

DateTime
DispatcherTimer
System.Diagnostics.Process
System.Diagnostics.Stopwatch
Run Code Online (Sandbox Code Playgroud)

但是没有发现这样的东西.这对我来说似乎是一个非常基本的功能.例如,在Mono上运行的Unity3D有一些熟悉的东西.我在这里想念一些实用工具吗?

提前致谢.

spe*_*der 39

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

..自从开始以来经过的时间:

DateTime.UtcNow - Process.GetCurrentProcess().StartTime.ToUniversalTime()
Run Code Online (Sandbox Code Playgroud)

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

Stopwatch sw = Stopwatch.StartNew();
Run Code Online (Sandbox Code Playgroud)

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