C#中精确计时的一点收获

原文地址:http://www.cnblogs.com/jintianhu/archive/2010/09/01/1815031.html

我的应用示例

 

    public static class ServerDateTime
{
static Stopwatch sw = new Stopwatch();
static CommonPresenter presenter = new CommonPresenter();
static DateTime LastCorrectTime = DateTime.Now;
static DateTime _now;
/// <summary>
/// 服务器时间(可能会有网络延迟),如果服务器访问失败则为本地时间
/// </summary>
public static DateTime Now
{
get { return LastCorrectTime.Add(sw.Elapsed); }
private set { _now = value; }
}

static ServerDateTime()
{
Now = DateTime.Now;
GetServerDateTime(null);
sw.Start();
}

static void GetServerDateTime(MethodInvoker callback)
{
presenter.RequestServerDateTime(serverdatetime =>
{
Now = serverdatetime;
LastCorrectTime = serverdatetime;
if (callback != null) callback();
});
}
}

 

你可能感兴趣的:(C#)