[聚合文章] 如何在ViewModel中每隔一段时间运行一次Event

c# 2015-01-15 1 阅读

有一种场景,每隔一段时间就要执行一次动作,比如从服务器上拉取内容。碰巧使用的又是C#+WPF的MVVM模式,需要在ViewModel里执行这种操作。该如何进行呢?

可以用DispatcherTimer,比如这样:

//The Constructure
public myViewModel 
{
  timer = new DispatcherTimer();
  timer.Interval = TimeSpan.FromSeconds(5); //Every 5 seconds do something
  timer.Tick += Tick_Event;
  timer.Start();
}

void Tick_Event(object sender, EventArgs e)
{
    //Do something you want
}

注:本文内容来自互联网,旨在为开发者提供分享、交流的平台。如有涉及文章版权等事宜,请你联系站长进行处理。