Windows Phone 8的计时器

rah*_*202 1 c# timer windows-phone-8

我希望以1分钟左右的间隔执行一个函数.如何在Windows Phone 8中实现此目的.

我不是在寻找后台代理.该应用程序将在前台运行.我有什么选择?

小智 10

您可以使用DispatcherTimer类

private DispatcherTimer dispatcherTimer;
    // Constructor
    public MainPage()
    {

        InitializeComponent();
        dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        dispatcherTimer.Start();

    }

  private void dispatcherTimer_Tick(object sender, EventArgs e)
  {
    //do whatever you want to do here
  }
Run Code Online (Sandbox Code Playgroud)

参考:(http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer ( v=vs.110 ) .aspx)


Abi*_*nZZ 5

试试这个

public void Start_timer()
{
    DispatcherTimer timer = new DispatcherTimer();
    timer.Tick += timer_Tick;
    timer.Interval = new TimeSpan(00, 0, 10);
    bool enabled = timer.IsEnabled;
    timer.Start();
}

void timer_Tick(object sender, object e)
{
    //function to execute
}
Run Code Online (Sandbox Code Playgroud)