我需要发送一个大约每分钟一次的服务器请求,以获得一个新的产品列表(如果它是通过网络更改)。
所以我用的是DispatcherTimer
public static void Start()
{
if (timer != null) return;
timer = new DispatcherTimer {Interval = TimeSpan.FromSeconds(0.1)};
timer.Tick += Run;
timer.Start();
}
private static async void Run(object sender, EventArgs e)
{
timer.Interval = TimeSpan.FromSeconds(60); // TODO" add dynamic changes here
timer.Stop();
** Do stuff
timer.Start();
}然而,有时,我需要强制更新。跑得对吗?
public static void ForceUpdate()
{
Run(null, null);
}编辑:我的意思是,如果做事情足够长,难道不是第二次通过计时器调用吗?或者也许我该用点别的东西来做这种工作?
发布于 2013-09-06 11:25:25
啊,嗯,这很简单
public static void ForceUpdate()
{
timer.Stop();
timer.Interval = TimeSpan.FromMilliseconds(10);
timer.Start();
}发布于 2013-09-06 08:45:12
编辑:插入一个变量,它应该存储最后的更新时间,并检查更新是否在一定的时间间隔内完成。
https://stackoverflow.com/questions/18653419
复制相似问题