我需要每分钟发送一次服务器请求,以获取新产品列表(如果它是通过网络更改的话)。
所以,我正在使用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);
}
编辑:我的意思是,如果做东西足够长,不会通过计时器第二次调用吗?或者也许我应该用其他东西来做这种工作?
答案 0 :(得分:0)
编辑:插入一个应存储上次更新时间的变量,并检查更新是否已在某个时间间隔内完成。
答案 1 :(得分:0)
啊,好吧,这很简单
public static void ForceUpdate()
{
timer.Stop();
timer.Interval = TimeSpan.FromMilliseconds(10);
timer.Start();
}