C# Windows Phone 8的计时器

C# Windows Phone 8的计时器,c#,windows-phone-8,timer,C#,Windows Phone 8,Timer,我希望每隔1分钟左右执行一个函数。如何在Windows Phone 8中实现这一点。我不需要。应用程序将在前台运行。我的选择是什么?一个选择可能是使用 只需在Tick事件上注册回调。可以使用一个选项 只需在Tick事件上注册回调。试试这个 public void Start_timer() { DispatcherTimer timer = new DispatcherTimer(); timer.Tick += timer_Tick; timer.Interval =

我希望每隔1分钟左右执行一个函数。如何在Windows Phone 8中实现这一点。

我不需要。应用程序将在前台运行。我的选择是什么?

一个选择可能是使用

只需在Tick事件上注册回调。

可以使用一个选项

只需在Tick事件上注册回调。

试试这个

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
}
试试这个

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
}

您可以使用Dispatchermer类

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
  }

请参阅:()

您可以使用Dispatcher类

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
  }

请参阅:()

您也可以使用
ThreadPoolTimer

TimeSpan period = TimeSpan.FromSeconds(60);

ThreadPoolTimer PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer((source) =>
{
    // TODO: Work

    Deployment.Current.Dispatcher.BeginInvoke(() => 
        {
            // UI update               
        });

}, period);

您还可以使用
ThreadPoolTimer

TimeSpan period = TimeSpan.FromSeconds(60);

ThreadPoolTimer PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer((source) =>
{
    // TODO: Work

    Deployment.Current.Dispatcher.BeginInvoke(() => 
        {
            // UI update               
        });

}, period);

为什么我更喜欢这个而不是简单计时器?为什么我更喜欢这个而不是简单计时器?@Chris Schiffhauer Bro我想在后台做这种工作。?你能建议如何在后台使用这个吗?@Chris Schiffhauer Bro我想在后台使用这种类型的工作。?你能建议如何在后台使用这个吗。?