Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
对于一个项目,我需要创建一个简单的计时器。按下按钮启动秒表,直到再次按下按钮。运行时间会一直显示,直到按下停止按钮。它只需要精确到百分之一秒。我的问题是 System.Diagnostics::Stopwatch 类是否是此应用程序的最佳选择?
不,该Stopwatch课程旨在用于非常精确(微秒或更好)的测量,而不是准确性(它可能与墙上时间不同步)。
Stopwatch
实现这一点的最佳方法是启动一个常规的 UI 计时器并记录开始时间(也许是DateTime.UtcNow)。每当计时器滴答作响时,只需用当前时间减去开始时间来更新显示。
DateTime.UtcNow